Index: ps/trunk/source/gui/CCheckBox.cpp =================================================================== --- ps/trunk/source/gui/CCheckBox.cpp (revision 14435) +++ ps/trunk/source/gui/CCheckBox.cpp (revision 14436) @@ -1,185 +1,186 @@ /* Copyright (C) 2013 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ /* CCheckBox */ #include "precompiled.h" #include "CCheckBox.h" #include "ps/CLogger.h" #include "ps/CStrIntern.h" #include "graphics/FontMetrics.h" //------------------------------------------------------------------- // Constructor / Destructor //------------------------------------------------------------------- CCheckBox::CCheckBox() { AddSetting(GUIST_float, "buffer_zone"); AddSetting(GUIST_CGUIString, "caption"); AddSetting(GUIST_int, "cell_id"); AddSetting(GUIST_bool, "checked"); AddSetting(GUIST_CStrW, "font"); AddSetting(GUIST_CStrW, "sound_disabled"); AddSetting(GUIST_CStrW, "sound_enter"); AddSetting(GUIST_CStrW, "sound_leave"); AddSetting(GUIST_CStrW, "sound_pressed"); AddSetting(GUIST_CStrW, "sound_released"); AddSetting(GUIST_CGUISpriteInstance, "sprite"); AddSetting(GUIST_CGUISpriteInstance, "sprite_over"); AddSetting(GUIST_CGUISpriteInstance, "sprite_pressed"); AddSetting(GUIST_CGUISpriteInstance, "sprite_disabled"); AddSetting(GUIST_CGUISpriteInstance, "sprite2"); AddSetting(GUIST_CGUISpriteInstance, "sprite2_over"); AddSetting(GUIST_CGUISpriteInstance, "sprite2_pressed"); AddSetting(GUIST_CGUISpriteInstance, "sprite2_disabled"); AddSetting(GUIST_float, "square_side"); AddSetting(GUIST_CColor, "textcolor"); AddSetting(GUIST_CColor, "textcolor_over"); AddSetting(GUIST_CColor, "textcolor_pressed"); AddSetting(GUIST_CColor, "textcolor_disabled"); AddSetting(GUIST_CStrW, "tooltip"); AddSetting(GUIST_CStr, "tooltip_style"); // Add text AddText(new SGUIText()); } CCheckBox::~CCheckBox() { } void CCheckBox::SetupText() { if (!GetGUI()) return; ENSURE(m_GeneratedTexts.size()==1); CStrW font; if (GUI::GetSetting(this, "font", font) != PSRETURN_OK || font.empty()) // Use the default if none is specified // TODO Gee: (2004-08-14) Default should not be hard-coded, but be in styles! font = L"default"; float square_side; GUI::GetSetting(this, "square_side", square_side); CGUIString caption; GUI::GetSetting(this, "caption", caption); float buffer_zone=0.f; GUI::GetSetting(this, "buffer_zone", buffer_zone); *m_GeneratedTexts[0] = GetGUI()->GenerateText(caption, font, m_CachedActualSize.GetWidth()-square_side, 0.f, this); } void CCheckBox::HandleMessage(SGUIMessage &Message) { // Important IGUIButtonBehavior::HandleMessage(Message); IGUITextOwner::HandleMessage(Message); switch (Message.type) { case GUIM_PRESSED: { bool checked; + // Switch to opposite. GUI::GetSetting(this, "checked", checked); checked = !checked; GUI::SetSetting(this, "checked", checked); - - } break; + break; + } default: break; } } void CCheckBox::Draw() { ////////// Gee: janwas, this is just temp to see it glDisable(GL_TEXTURE_2D); ////////// float square_side, buffer_zone; CStrW font_name; bool checked; int cell_id; GUI::GetSetting(this, "square_side", square_side); GUI::GetSetting(this, "buffer_zone", buffer_zone); GUI::GetSetting(this, "font", font_name); GUI::GetSetting(this, "checked", checked); GUI::GetSetting(this, "cell_id", cell_id); // Get line height CFontMetrics font (CStrIntern(font_name.ToUTF8())); float line_height = (float)font.GetHeight(); float bz = GetBufferedZ(); // Get square CRect rect; rect.left = m_CachedActualSize.left; rect.right = rect.left + square_side; if (square_side >= line_height) rect.top = m_CachedActualSize.top; else rect.top = m_CachedActualSize.top + line_height/2.f - square_side/2.f; rect.bottom = rect.top + square_side; CGUISpriteInstance *sprite, *sprite_over, *sprite_pressed, *sprite_disabled; if (checked) { GUI::GetSettingPointer(this, "sprite2", sprite); GUI::GetSettingPointer(this, "sprite2_over", sprite_over); GUI::GetSettingPointer(this, "sprite2_pressed", sprite_pressed); GUI::GetSettingPointer(this, "sprite2_disabled", sprite_disabled); } else { GUI::GetSettingPointer(this, "sprite", sprite); GUI::GetSettingPointer(this, "sprite_over", sprite_over); GUI::GetSettingPointer(this, "sprite_pressed", sprite_pressed); GUI::GetSettingPointer(this, "sprite_disabled", sprite_disabled); } DrawButton(rect, bz, *sprite, *sprite_over, *sprite_pressed, *sprite_disabled, cell_id); CColor color = ChooseColor(); CPos text_pos(m_CachedActualSize.left + square_side + buffer_zone, m_CachedActualSize.top); if (square_side > line_height) text_pos.y += square_side/2.f - line_height/2.f; DrawText(0, color, text_pos, bz+0.1f, m_CachedActualSize); } Index: ps/trunk/source/gui/IGUIButtonBehavior.cpp =================================================================== --- ps/trunk/source/gui/IGUIButtonBehavior.cpp (revision 14435) +++ ps/trunk/source/gui/IGUIButtonBehavior.cpp (revision 14436) @@ -1,220 +1,211 @@ /* Copyright (C) 2013 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ /* IGUIButtonBehavior */ #include "precompiled.h" #include "GUI.h" #include "soundmanager/ISoundManager.h" //------------------------------------------------------------------- // Constructor / Destructor //------------------------------------------------------------------- IGUIButtonBehavior::IGUIButtonBehavior() : m_Pressed(false) { } IGUIButtonBehavior::~IGUIButtonBehavior() { } void IGUIButtonBehavior::HandleMessage(SGUIMessage &Message) { bool enabled; GUI::GetSetting(this, "enabled", enabled); + CStrW soundPath; // TODO Gee: easier access functions switch (Message.type) { case GUIM_MOUSE_ENTER: { if (!enabled) break; - CStrW soundPath; if (g_SoundManager && GUI::GetSetting(this, "sound_enter", soundPath) == PSRETURN_OK && !soundPath.empty()) g_SoundManager->PlayAsUI(soundPath.c_str(), false); } break; case GUIM_MOUSE_LEAVE: { if (!enabled) break; - CStrW soundPath; if (g_SoundManager && GUI::GetSetting(this, "sound_leave", soundPath) == PSRETURN_OK && !soundPath.empty()) g_SoundManager->PlayAsUI(soundPath.c_str(), false); } break; case GUIM_MOUSE_DBLCLICK_LEFT: case GUIM_MOUSE_PRESS_LEFT: { if (!enabled) { - CStrW soundPath; if (g_SoundManager && GUI::GetSetting(this, "sound_disabled", soundPath) == PSRETURN_OK && !soundPath.empty()) g_SoundManager->PlayAsUI(soundPath.c_str(), false); break; } - CStrW soundPath; if (g_SoundManager && GUI::GetSetting(this, "sound_pressed", soundPath) == PSRETURN_OK && !soundPath.empty()) g_SoundManager->PlayAsUI(soundPath.c_str(), false); // Button was clicked SendEvent(GUIM_PRESSED, "press"); if (Message.type == GUIM_MOUSE_DBLCLICK_LEFT) { // Button was clicked a second time. We can't tell if the button // expects to receive doublepress events or just a second press // event, so send both of them (and assume the extra unwanted press // is harmless on buttons that expect doublepress) SendEvent(GUIM_DOUBLE_PRESSED, "doublepress"); } m_Pressed = true; } break; case GUIM_MOUSE_DBLCLICK_RIGHT: case GUIM_MOUSE_PRESS_RIGHT: { if (!enabled) { - CStrW soundPath; if (g_SoundManager && GUI::GetSetting(this, "sound_disabled", soundPath) == PSRETURN_OK && !soundPath.empty()) g_SoundManager->PlayAsUI(soundPath.c_str(), false); break; } - CStrW soundPath; if (g_SoundManager && GUI::GetSetting(this, "sound_pressed", soundPath) == PSRETURN_OK && !soundPath.empty()) g_SoundManager->PlayAsUI(soundPath.c_str(), false); // Button was right-clicked SendEvent(GUIM_PRESSED_MOUSE_RIGHT, "pressright"); if (Message.type == GUIM_MOUSE_DBLCLICK_RIGHT) { // Button was clicked a second time. We can't tell if the button // expects to receive doublepress events or just a second press // event, so send both of them (and assume the extra unwanted press // is harmless on buttons that expect doublepress) SendEvent(GUIM_DOUBLE_PRESSED_MOUSE_RIGHT, "doublepressright"); } m_PressedRight = true; } break; case GUIM_MOUSE_RELEASE_RIGHT: { if (!enabled) break; if (m_PressedRight) { m_PressedRight = false; - CStrW soundPath; if (g_SoundManager && GUI::GetSetting(this, "sound_released", soundPath) == PSRETURN_OK && !soundPath.empty()) g_SoundManager->PlayAsUI(soundPath.c_str(), false); } } break; case GUIM_MOUSE_RELEASE_LEFT: { if (!enabled) break; if (m_Pressed) { m_Pressed = false; - CStrW soundPath; if (g_SoundManager && GUI::GetSetting(this, "sound_released", soundPath) == PSRETURN_OK && !soundPath.empty()) g_SoundManager->PlayAsUI(soundPath.c_str(), false); } } break; default: break; } } CColor IGUIButtonBehavior::ChooseColor() { CColor color, color2; // Yes, the object must possess these settings. They are standard GUI::GetSetting(this, "textcolor", color); bool enabled; GUI::GetSetting(this, "enabled", enabled); if (!enabled) { GUI::GetSetting(this, "textcolor_disabled", color2); return GUI<>::FallBackColor(color2, color); } else if (m_MouseHovering) { if (m_Pressed) { GUI::GetSetting(this, "textcolor_pressed", color2); return GUI<>::FallBackColor(color2, color); } else { GUI::GetSetting(this, "textcolor_over", color2); return GUI<>::FallBackColor(color2, color); } } else return color; } void IGUIButtonBehavior::DrawButton(const CRect &rect, const float &z, CGUISpriteInstance& sprite, CGUISpriteInstance& sprite_over, CGUISpriteInstance& sprite_pressed, CGUISpriteInstance& sprite_disabled, int cell_id) { if (GetGUI()) { bool enabled; GUI::GetSetting(this, "enabled", enabled); if (!enabled) - { GetGUI()->DrawSprite(GUI<>::FallBackSprite(sprite_disabled, sprite), cell_id, z, rect); - } - else - if (m_MouseHovering) + else if (m_MouseHovering) { if (m_Pressed) GetGUI()->DrawSprite(GUI<>::FallBackSprite(sprite_pressed, sprite), cell_id, z, rect); else GetGUI()->DrawSprite(GUI<>::FallBackSprite(sprite_over, sprite), cell_id, z, rect); } - else GetGUI()->DrawSprite(sprite, cell_id, z, rect); + else + GetGUI()->DrawSprite(sprite, cell_id, z, rect); } }