Ticket #3788: gui_subsystem_code_refactoring.2.patch
File gui_subsystem_code_refactoring.2.patch, 31.0 KB (added by , 8 years ago) |
---|
-
gui/CButton.cpp
53 53 { 54 54 } 55 55 56 void CButton::ResetStates() { IGUIButtonBehavior::ResetStates(); } 57 56 58 void CButton::SetupText() 57 59 { 58 60 if (!GetGUI()) -
gui/CButton.h
37 37 /** 38 38 * @see IGUIObject#ResetStates() 39 39 */ 40 virtual void ResetStates() { IGUIButtonBehavior::ResetStates(); }40 virtual void ResetStates(); 41 41 42 42 /** 43 43 * @see IGUIObject#HandleMessage() -
gui/CCheckBox.cpp
59 59 { 60 60 } 61 61 62 void CCheckBox::ResetStates() { IGUIButtonBehavior::ResetStates(); } 63 62 64 void CCheckBox::SetupText() 63 65 { 64 66 if (!GetGUI()) -
gui/CCheckBox.h
38 38 /** 39 39 * @see IGUIObject#ResetStates() 40 40 */ 41 virtual void ResetStates() { IGUIButtonBehavior::ResetStates(); }41 virtual void ResetStates(); 42 42 43 43 /** 44 44 * @see IGUIObject#HandleMessage() -
gui/CDropDown.cpp
470 470 GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite2_pressed", sprite2_second); 471 471 GetGUI()->DrawSprite(GUI<>::FallBackSprite(*sprite2_second, *sprite2), cell_id, bz+0.05f, rect); 472 472 } 473 else if ( m_MouseHovering)473 else if (IsMouseHovering()) 474 474 { 475 475 GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite2_over", sprite2_second); 476 476 GetGUI()->DrawSprite(GUI<>::FallBackSprite(*sprite2_second, *sprite2), cell_id, bz+0.05f, rect); -
gui/CGUI.cpp
170 170 case SDL_BUTTON_LEFT: 171 171 if (pNearest) 172 172 { 173 double timeElapsed = timer_Time() - pNearest-> m_LastClickTime[SDL_BUTTON_LEFT];174 pNearest-> m_LastClickTime[SDL_BUTTON_LEFT] = timer_Time();173 double timeElapsed = timer_Time() - pNearest->GetLastClickTime(SDL_BUTTON_LEFT); 174 pNearest->SetLastClickTime(SDL_BUTTON_LEFT, timer_Time() ); 175 175 176 176 //Double click? 177 177 if (timeElapsed < SELECT_DBLCLICK_RATE) … … 187 187 case SDL_BUTTON_RIGHT: 188 188 if (pNearest) 189 189 { 190 double timeElapsed = timer_Time() - pNearest-> m_LastClickTime[SDL_BUTTON_RIGHT];191 pNearest-> m_LastClickTime[SDL_BUTTON_RIGHT] = timer_Time();190 double timeElapsed = timer_Time() - pNearest->GetLastClickTime(SDL_BUTTON_RIGHT); 191 pNearest->SetLastClickTime(SDL_BUTTON_RIGHT, timer_Time()); 192 192 193 193 //Double click? 194 194 if (timeElapsed < SELECT_DBLCLICK_RATE) -
gui/CInput.cpp
75 75 { 76 76 } 77 77 78 void CInput::ResetStates() { IGUIScrollBarOwner::ResetStates(); } 79 78 80 void CInput::UpdateBufferPositionSetting() 79 81 { 80 int* bufferPos = (int*) m_Settings["buffer_position"].m_pSetting;82 int* bufferPos = (int*)RefSetting("buffer_position").m_pSetting; 81 83 *bufferPos = m_iBufferPos; 82 84 } 83 85 84 86 void CInput::ClearComposedText() 85 87 { 86 CStrW* pCaption = (CStrW*) m_Settings["caption"].m_pSetting;88 CStrW* pCaption = (CStrW*)RefSetting("caption").m_pSetting; 87 89 pCaption->erase(m_iInsertPos, m_iComposedLength); 88 90 m_iBufferPos = m_iInsertPos; 89 91 UpdateBufferPositionSetting(); … … 106 108 else if (ev->ev.type == SDL_TEXTINPUT) 107 109 { 108 110 // Text has been committed, either single key presses or through an IME 109 CStrW* pCaption = (CStrW*) m_Settings["caption"].m_pSetting;111 CStrW* pCaption = (CStrW*)RefSetting("caption").m_pSetting; 110 112 std::wstring text = wstring_from_utf8(ev->ev.text.text); 111 113 112 114 m_WantedX = 0.0f; … … 139 141 { 140 142 // Text is being composed with an IME 141 143 // TODO: indicate this by e.g. underlining the uncommitted text 142 CStrW* pCaption = (CStrW*) m_Settings["caption"].m_pSetting;144 CStrW* pCaption = (CStrW*)RefSetting("caption").m_pSetting; 143 145 const char* rawText = ev->ev.edit.text; 144 146 int rawLength = strlen(rawText); 145 147 std::wstring wtext = wstring_from_utf8(rawText); … … 188 190 // Since the GUI framework doesn't handle to set settings 189 191 // in Unicode (CStrW), we'll simply retrieve the actual 190 192 // pointer and edit that. 191 CStrW* pCaption = (CStrW*) m_Settings["caption"].m_pSetting;193 CStrW* pCaption = (CStrW*)RefSetting("caption").m_pSetting; 192 194 bool shiftKeyPressed = g_keys[SDLK_RSHIFT] || g_keys[SDLK_LSHIFT]; 193 195 194 196 int szChar = 0; … … 527 529 528 530 InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event_* ev) 529 531 { 530 CStrW* pCaption = (CStrW*) m_Settings["caption"].m_pSetting;532 CStrW* pCaption = (CStrW*)RefSetting("caption").m_pSetting; 531 533 bool shiftKeyPressed = g_keys[SDLK_RSHIFT] || g_keys[SDLK_LSHIFT]; 532 534 533 535 std::string hotkey = static_cast<const char*>(ev->ev.user.data1); … … 882 884 if (m_ComposingText) 883 885 break; 884 886 885 CStrW* pCaption = (CStrW*) m_Settings["caption"].m_pSetting;887 CStrW* pCaption = (CStrW*)RefSetting("caption").m_pSetting; 886 888 887 889 if (pCaption->empty()) 888 890 break; … … 1135 1137 mask_char = maskStr[0]; 1136 1138 } 1137 1139 else 1138 pCaption = (CStrW*) m_Settings["caption"].m_pSetting;1140 pCaption = (CStrW*)RefSetting("caption").m_pSetting; 1139 1141 1140 1142 CGUISpriteInstance* sprite = NULL; 1141 1143 CGUISpriteInstance* sprite_selectarea = NULL; … … 1952 1954 1953 1955 void CInput::DeleteCurSelection() 1954 1956 { 1955 CStrW* pCaption = (CStrW*) m_Settings["caption"].m_pSetting;1957 CStrW* pCaption = (CStrW*)RefSetting("caption").m_pSetting; 1956 1958 1957 1959 int virtualFrom; 1958 1960 int virtualTo; -
gui/CInput.h
43 43 /** 44 44 * @see IGUIObject#ResetStates() 45 45 */ 46 virtual void ResetStates() { IGUIScrollBarOwner::ResetStates(); }46 virtual void ResetStates(); 47 47 48 48 // Check where the mouse is hovering, and get the appropriate text position. 49 49 // return is the text-position index. -
gui/CList.cpp
65 65 { 66 66 } 67 67 68 void CList::ResetStates() { IGUIScrollBarOwner::ResetStates(); } 69 68 70 void CList::SetupText() 69 71 { 70 72 if (!GetGUI()) -
gui/CList.h
40 40 /** 41 41 * @see IGUIObject#ResetStates() 42 42 */ 43 virtual void ResetStates() { IGUIScrollBarOwner::ResetStates(); }43 virtual void ResetStates(); 44 44 45 45 /** 46 46 * Adds an item last to the list. -
gui/CText.cpp
62 62 { 63 63 } 64 64 65 void CText::ResetStates() { IGUIScrollBarOwner::ResetStates(); } 66 65 67 void CText::SetupText() 66 68 { 67 69 if (!GetGUI()) -
gui/CText.h
36 36 /** 37 37 * @see IGUIObject#ResetStates() 38 38 */ 39 virtual void ResetStates() { IGUIScrollBarOwner::ResetStates(); }39 virtual void ResetStates(); 40 40 41 41 /** 42 42 * Test if mouse position is over an icon -
gui/GUIutil.cpp
320 320 { 321 321 ENSURE(pObject != NULL); 322 322 323 std::map<CStr, SGUISetting>::const_iterator it = pObject->m_Settings.find(Setting);324 if ( it == pObject->m_Settings.end())323 324 if (!pObject->SettingExists(Setting)) 325 325 { 326 326 LOGWARNING("setting %s was not found on object %s", 327 327 Setting.c_str(), … … 329 329 return PSRETURN_GUI_InvalidSetting; 330 330 } 331 331 332 if (it->second.m_pSetting == NULL) 332 auto& ref = pObject->RefSetting(Setting); 333 if (ref.m_pSetting == NULL) 333 334 return PSRETURN_GUI_InvalidSetting; 334 335 335 336 #ifndef NDEBUG … … 337 338 #endif 338 339 339 340 // Get value 340 Value = (T*)( it->second.m_pSetting);341 Value = (T*)(ref.m_pSetting); 341 342 342 343 return PSRETURN_OK; 343 344 } … … 382 383 #endif 383 384 384 385 // Set value 385 *(T*)pObject-> m_Settings[Setting].m_pSetting = Value;386 *(T*)pObject->RefSetting(Setting).m_pSetting = Value; 386 387 387 388 // 388 389 // Some settings needs special attention at change -
gui/IGUIButtonBehavior.cpp
146 146 GUI<CColor>::GetSetting(this, "textcolor_disabled", color2); 147 147 return GUI<>::FallBackColor(color2, color); 148 148 } 149 else if ( m_MouseHovering)149 else if (IsMouseHovering()) 150 150 { 151 151 if (m_Pressed) 152 152 { … … 163 163 return color; 164 164 } 165 165 166 void IGUIButtonBehavior::ResetStates() 167 { 168 // Notify the gui that we aren't hovered anymore 169 UpdateMouseOver(NULL); 170 m_Pressed = false; 171 m_PressedRight = false; 172 } 173 166 174 void IGUIButtonBehavior::DrawButton(const CRect& rect, const float& z, CGUISpriteInstance& sprite, CGUISpriteInstance& sprite_over, CGUISpriteInstance& sprite_pressed, CGUISpriteInstance& sprite_disabled, int cell_id) 167 175 { 168 176 if (!GetGUI()) … … 173 181 174 182 if (!enabled) 175 183 GetGUI()->DrawSprite(GUI<>::FallBackSprite(sprite_disabled, sprite), cell_id, z, rect); 176 else if ( m_MouseHovering)184 else if (IsMouseHovering()) 177 185 { 178 186 if (m_Pressed) 179 187 GetGUI()->DrawSprite(GUI<>::FallBackSprite(sprite_pressed, sprite), cell_id, z, rect); -
gui/IGUIButtonBehavior.h
89 89 /** 90 90 * @see IGUIObject#ResetStates() 91 91 */ 92 virtual void ResetStates() 93 { 94 // Notify the gui that we aren't hovered anymore 95 UpdateMouseOver(NULL); 96 m_Pressed = false; 97 m_PressedRight = false; 98 } 92 virtual void ResetStates(); 99 93 100 94 /** 101 95 * Everybody knows how a button works, you don't simply press it, -
gui/IGUIObject.cpp
24 24 #include "ps/CLogger.h" 25 25 #include "scriptinterface/ScriptInterface.h" 26 26 27 class IGuiObjectImpl 28 { 29 public: 30 // Name of object 31 CStr Name; 27 32 33 // Constructed on the heap, will be destroyed along with the the object 34 // TODO Gee: really the above? 35 vector_pObjects Children; 36 37 // Pointer to parent 38 IGUIObject *Parent = nullptr; 39 40 //This represents the last click time for each mouse button 41 std::vector<double> LastClickTime = std::vector<double>(6); 42 43 // Is mouse hovering the object? used with the function MouseOver() 44 bool IsMouseHovering = false; 45 46 // An object can't function stand alone 47 CGUI *GUI = nullptr; 48 49 // Internal storage for registered script handlers. 50 std::map<CStr, JS::Heap<JSObject*> > ScriptHandlers; 51 52 // Cached JSObject representing this GUI object 53 DefPersistentRooted<JSObject*> JSObject; 54 55 /** 56 * Settings pool, all an object's settings are located here 57 * If a derived object has got more settings that the base 58 * settings, it's because they have a new version of the 59 * function SetupSettings(). 60 * 61 * @see SetupSettings() 62 */ 63 std::map<CStr, SGUISetting> Settings; 64 }; 65 28 66 IGUIObject::IGUIObject() 29 : m _pGUI(NULL), m_pParent(NULL), m_MouseHovering(false), m_LastClickTime()67 : m(new IGuiObjectImpl) 30 68 { 31 69 AddSetting(GUIST_bool, "enabled"); 32 70 AddSetting(GUIST_bool, "hidden"); … … 47 85 GUI<bool>::SetSetting(this, "absolute", true); 48 86 } 49 87 88 const CStr& IGUIObject::GetName() const { return m->Name; } 89 void IGUIObject::SetName(const CStr& Name) { m->Name = Name; } 90 vector_pObjects::iterator IGUIObject::begin() { return m->Children.begin(); } 91 vector_pObjects::iterator IGUIObject::end() { return m->Children.end(); } 92 void IGUIObject::SetParent(IGUIObject* pParent) { m->Parent = pParent; } 93 double IGUIObject::GetLastClickTime(int name) const { return m->LastClickTime[name]; } //TODO: possible need check for array range 94 void IGUIObject::SetLastClickTime(int name, double time) { m->LastClickTime[name] = time; } //TODO: possible need check for array range 95 bool IGUIObject::IsMouseHovering() const { return m->IsMouseHovering; } 96 void IGUIObject::SetMouseHovering(bool hover) { m->IsMouseHovering = hover; } 97 CGUI* IGUIObject::GetGUI() { return m->GUI; } 98 const CGUI* IGUIObject::GetGUI() const { return m->GUI; } 99 SGUISetting& IGUIObject::RefSetting(const CStr& name) { return m->Settings[name]; } 100 const SGUISetting& IGUIObject::RefSetting(const CStr& name) const { return m->Settings[name]; } 101 std::map<CStr, SGUISetting>& IGUIObject::GetSettings() { return m->Settings; } 102 const std::map<CStr, SGUISetting>& IGUIObject::GetSettings() const { return m->Settings; } 103 104 JS::Heap<JSObject*>* IGUIObject::GetJsEventHandler(const CStr& eventName) 105 { 106 auto it = m->ScriptHandlers.find(eventName); 107 if (it == m->ScriptHandlers.end()) 108 return nullptr; 109 110 return &it->second; 111 } 112 113 void IGUIObject::ResetStates() 114 { 115 // Notify the gui that we aren't hovered anymore 116 UpdateMouseOver(NULL); 117 } 118 50 119 IGUIObject::~IGUIObject() 51 120 { 52 for (const std::pair<CStr, SGUISetting>& p : m _Settings)121 for (const std::pair<CStr, SGUISetting>& p : m->Settings) 53 122 switch (p.second.m_Type) 54 123 { 55 124 // delete() needs to know the type of the variable - never delete a void* … … 60 129 debug_warn(L"Invalid setting type"); 61 130 } 62 131 63 if ( m_pGUI)64 JS_RemoveExtraGCRootsTracer( m_pGUI->GetScriptInterface()->GetJSRuntime(), Trace, this);132 if (GetGUI()) 133 JS_RemoveExtraGCRootsTracer(GetGUI()->GetScriptInterface()->GetJSRuntime(), Trace, this); 65 134 } 66 135 67 136 //------------------------------------------------------------------- … … 69 138 //------------------------------------------------------------------- 70 139 void IGUIObject::SetGUI(CGUI* const& pGUI) 71 140 { 72 if (!m _pGUI)141 if (!m->GUI) 73 142 JS_AddExtraGCRootsTracer(pGUI->GetScriptInterface()->GetJSRuntime(), Trace, this); 74 m _pGUI = pGUI;143 m->GUI = pGUI; 75 144 } 76 145 146 vector_pObjects& IGUIObject::GetChildren() { return m->Children; } 147 77 148 void IGUIObject::AddChild(IGUIObject* pChild) 78 149 { 79 150 // ENSURE(pChild); … … 80 151 81 152 pChild->SetParent(this); 82 153 83 m_Children.push_back(pChild);154 GetChildren().push_back(pChild); 84 155 85 156 // If this (not the child) object is already attached 86 157 // to a CGUI, it pGUI pointer will be non-null. … … 99 170 { 100 171 // If anything went wrong, reverse what we did and throw 101 172 // an exception telling it never added a child 102 m_Children.erase(m_Children.end()-1);173 GetChildren().pop_back(); 103 174 104 175 throw; 105 176 } … … 110 181 void IGUIObject::AddToPointersMap(map_pObjects& ObjectMap) 111 182 { 112 183 // Just don't do anything about the top node 113 if (m _pParent == NULL)184 if (m->Parent == NULL) 114 185 return; 115 186 116 187 // Now actually add this one 117 188 // notice we won't add it if it's doesn't have any parent 118 189 // (i.e. being the base object) 119 if (m_Name.empty()) 190 const auto& name = GetName(); 191 if (name.empty()) 120 192 { 121 193 throw PSERROR_GUI_ObjectNeedsName(); 122 194 } 123 if (ObjectMap.count( m_Name) > 0)195 if (ObjectMap.count(name) > 0) 124 196 { 125 throw PSERROR_GUI_NameAmbiguity( m_Name.c_str());197 throw PSERROR_GUI_NameAmbiguity(name.c_str()); 126 198 } 127 199 else 128 200 { 129 ObjectMap[ m_Name] = this;201 ObjectMap[name] = this; 130 202 } 131 203 } 132 204 … … 138 210 void IGUIObject::AddSetting(const EGUISettingType& Type, const CStr& Name) 139 211 { 140 212 // Is name already taken? 141 if (m _Settings.count(Name) >= 1)213 if (m->Settings.count(Name) >= 1) 142 214 return; 143 215 144 216 // Construct, and set type 145 m _Settings[Name].m_Type = Type;217 m->Settings[Name].m_Type = Type; 146 218 147 219 switch (Type) 148 220 { 149 221 #define TYPE(type) \ 150 222 case GUIST_##type: \ 151 m _Settings[Name].m_pSetting = new type(); \223 m->Settings[Name].m_pSetting = new type(); \ 152 224 break; 153 225 154 226 // Construct the setting. … … 189 261 // Check if this is the object being hovered. 190 262 if (pMouseOver == this) 191 263 { 192 if (! m_MouseHovering)264 if (!IsMouseHovering()) 193 265 { 194 266 // It wasn't hovering, so that must mean it just entered 195 267 SendEvent(GUIM_MOUSE_ENTER, "mouseenter"); … … 196 268 } 197 269 198 270 // Either way, set to true 199 m_MouseHovering = true;271 SetMouseHovering(true); 200 272 201 273 // call mouse over 202 274 SendEvent(GUIM_MOUSE_OVER, "mousemove"); … … 203 275 } 204 276 else // Some other object (or none) is hovered 205 277 { 206 if ( m_MouseHovering)278 if (IsMouseHovering()) 207 279 { 208 m_MouseHovering = false;280 SetMouseHovering(false); 209 281 SendEvent(GUIM_MOUSE_LEAVE, "mouseleave"); 210 282 } 211 283 } … … 218 290 // we need to make no further updates on this function 219 291 // in derived classes. 220 292 //return (GetSettingsInfo().count(Setting) >= 1); 221 return (m _Settings.count(Setting) >= 1);293 return (m->Settings.count(Setting) >= 1); 222 294 } 223 295 224 296 PSRETURN IGUIObject::SetSetting(const CStr& Setting, const CStrW& Value, const bool& SkipMessage) … … 227 299 return PSRETURN_GUI_InvalidSetting; 228 300 229 301 // Get setting 230 SGUISetting set = m _Settings[Setting];302 SGUISetting set = m->Settings[Setting]; 231 303 232 304 #define TYPE(type) \ 233 305 else if (set.m_Type == GUIST_##type) \ … … 259 331 if (!SettingExists(Setting)) 260 332 return LogInvalidSettings(Setting); 261 333 262 if (m_Settings.find(Setting) == m_Settings.end()) 263 return LogInvalidSettings(Setting); 334 Type = RefSetting(Setting).m_Type; 264 335 265 Type = m_Settings.find(Setting)->second.m_Type;266 267 336 return PSRETURN_OK; 268 337 } 269 338 … … 292 361 { 293 362 // Important, we're not using GetParent() for these 294 363 // checks, that could screw it up 295 if (m _pParent)364 if (m->Parent) 296 365 { 297 if (m _pParent->m_pParent == NULL)366 if (m->Parent->m->Parent == NULL) 298 367 return NULL; 299 368 } 300 369 301 return m _pParent;370 return m->Parent; 302 371 } 303 372 304 373 void IGUIObject::UpdateCachedSize() … … 315 384 // If absolute="false" and the object has got a parent, 316 385 // use its cached size instead of the screen. Notice 317 386 // it must have just been cached for it to work. 318 if (absolute == false && m _pParent && !IsRootObject())319 m_CachedActualSize = ca.GetClientArea(m _pParent->m_CachedActualSize);387 if (absolute == false && m->Parent && !IsRootObject()) 388 m_CachedActualSize = ca.GetClientArea(m->Parent->m_CachedActualSize); 320 389 else 321 390 m_CachedActualSize = ca.GetClientArea(CRect(0.f, 0.f, g_xres * g_GuiScale, g_yres * g_GuiScale)); 322 391 … … 433 502 { 434 503 // m_ScriptHandlers is only rooted after SetGUI() has been called (which sets up the GC trace callbacks), 435 504 // so we can't safely store objects in it if the GUI hasn't been set yet. 436 ENSURE(m _pGUI && "A GUI must be associated with the GUIObject before adding ScriptHandlers!");437 m _ScriptHandlers[Action] = JS::Heap<JSObject*>(Function);505 ENSURE(m->GUI && "A GUI must be associated with the GUIObject before adding ScriptHandlers!"); 506 m->ScriptHandlers[Action] = JS::Heap<JSObject*>(Function); 438 507 } 439 508 440 509 InReaction IGUIObject::SendEvent(EGUIMessageType type, const CStr& EventName) … … 441 510 { 442 511 PROFILE2_EVENT("gui event"); 443 512 PROFILE2_ATTR("type: %s", EventName.c_str()); 444 PROFILE2_ATTR("object: %s", m_Name.c_str());513 PROFILE2_ATTR("object: %s", GetName().c_str()); 445 514 446 515 SGUIMessage msg(type); 447 516 HandleMessage(msg); … … 453 522 454 523 void IGUIObject::ScriptEvent(const CStr& Action) 455 524 { 456 std::map<CStr, JS::Heap<JSObject*>>::iterator it = m_ScriptHandlers.find(Action);457 if (it == m _ScriptHandlers.end())525 auto it = m->ScriptHandlers.find(Action); 526 if (it == m->ScriptHandlers.end()) 458 527 return; 459 528 460 JSContext* cx = m _pGUI->GetScriptInterface()->GetContext();529 JSContext* cx = m->GUI->GetScriptInterface()->GetContext(); 461 530 JSAutoRequest rq(cx); 462 531 463 532 // Set up the 'mouse' parameter 464 533 JS::RootedValue mouse(cx); 465 m _pGUI->GetScriptInterface()->Eval("({})", &mouse);466 m _pGUI->GetScriptInterface()->SetProperty(mouse, "x", m_pGUI->m_MousePos.x, false);467 m _pGUI->GetScriptInterface()->SetProperty(mouse, "y", m_pGUI->m_MousePos.y, false);468 m _pGUI->GetScriptInterface()->SetProperty(mouse, "buttons", m_pGUI->m_MouseButtons, false);534 m->GUI->GetScriptInterface()->Eval("({})", &mouse); 535 m->GUI->GetScriptInterface()->SetProperty(mouse, "x", m->GUI->m_MousePos.x, false); 536 m->GUI->GetScriptInterface()->SetProperty(mouse, "y", m->GUI->m_MousePos.y, false); 537 m->GUI->GetScriptInterface()->SetProperty(mouse, "buttons", m->GUI->m_MouseButtons, false); 469 538 470 539 JS::AutoValueVector paramData(cx); 471 540 paramData.append(mouse); … … 482 551 483 552 void IGUIObject::ScriptEvent(const CStr& Action, JS::HandleValue Argument) 484 553 { 485 std::map<CStr, JS::Heap<JSObject*>>::iterator it = m_ScriptHandlers.find(Action);486 if (it == m _ScriptHandlers.end())554 auto it = m->ScriptHandlers.find(Action); 555 if (it == m->ScriptHandlers.end()) 487 556 return; 488 557 489 JSContext* cx = m _pGUI->GetScriptInterface()->GetContext();558 JSContext* cx = m->GUI->GetScriptInterface()->GetContext(); 490 559 JSAutoRequest rq(cx); 491 560 JS::AutoValueVector paramData(cx); 492 561 paramData.append(Argument.get()); … … 502 571 503 572 JSObject* IGUIObject::GetJSObject() 504 573 { 505 JSContext* cx = m _pGUI->GetScriptInterface()->GetContext();574 JSContext* cx = m->GUI->GetScriptInterface()->GetContext(); 506 575 JSAutoRequest rq(cx); 507 576 // Cache the object when somebody first asks for it, because otherwise 508 577 // we end up doing far too much object allocation. TODO: Would be nice to 509 578 // not have these objects hang around forever using up memory, though. 510 if (m _JSObject.uninitialized())579 if (m->JSObject.uninitialized()) 511 580 { 512 m _JSObject.set(cx, m_pGUI->GetScriptInterface()->CreateCustomObject("GUIObject"));513 JS_SetPrivate(m _JSObject.get(), this);581 m->JSObject.set(cx, m->GUI->GetScriptInterface()->CreateCustomObject("GUIObject")); 582 JS_SetPrivate(m->JSObject.get(), this); 514 583 } 515 return m _JSObject.get();584 return m->JSObject.get(); 516 585 } 517 586 518 587 CStr IGUIObject::GetPresentableName() const … … 519 588 { 520 589 // __internal(), must be at least 13 letters to be able to be 521 590 // an internal name 522 if (m_Name.length() <= 12) 523 return m_Name; 591 const auto& name = GetName(); 592 if (name.length() <= 12) 593 return name; 524 594 525 if ( m_Name.substr(0, 10) == "__internal")595 if (name.substr(0, 10) == "__internal") 526 596 return CStr("[unnamed object]"); 527 597 else 528 return m_Name;598 return name; 529 599 } 530 600 531 601 void IGUIObject::SetFocus() … … 540 610 541 611 bool IGUIObject::IsRootObject() const 542 612 { 543 return GetGUI() != 0 && m _pParent == GetGUI()->m_BaseObject;613 return GetGUI() != 0 && m->Parent == GetGUI()->m_BaseObject; 544 614 } 545 615 546 616 void IGUIObject::TraceMember(JSTracer* trc) 547 617 { 548 for ( std::pair<const CStr, JS::Heap<JSObject*>>& handler : m_ScriptHandlers)618 for (auto& handler : m->ScriptHandlers) 549 619 JS_CallHeapObjectTracer(trc, &handler.second, "IGUIObject::m_ScriptHandlers"); 550 620 } 551 621 -
gui/IGUIObject.h
110 110 111 111 ////////////////////////////////////////////////////////// 112 112 113 class IGuiObjectImpl; 113 114 /** 114 115 * GUI object such as a button or an input-box. 115 116 * Abstract data type ! … … 155 156 //@{ 156 157 157 158 /// Get object name, name is unique 158 const CStr& GetName() const { return m_Name; }159 const CStr& GetName() const; 159 160 160 161 /// Get object name 161 void SetName(const CStr& Name) { m_Name = Name; }162 void SetName(const CStr& Name); 162 163 164 CGUI* GetGUI(); 165 const CGUI* GetGUI() const; 166 167 /** 168 * Take focus! 169 */ 170 void SetFocus(); 171 163 172 // Get Presentable name. 164 173 // Will change all internally set names to something like "<unnamed object>" 165 174 CStr GetPresentableName() const; 166 175 167 176 /** 177 * This function is called with different messages 178 * for instance when the mouse enters the object. 179 * 180 * @param Message GUI Message 181 */ 182 virtual void HandleMessage(SGUIMessage& UNUSED(Message)) {} 183 184 /** 168 185 * Adds object and its children to the map, it's name being the 169 186 * first part, and the second being itself. 170 187 * … … 201 218 //-------------------------------------------------------- 202 219 //@{ 203 220 204 vector_pObjects::iterator begin() { return m_Children.begin(); }205 vector_pObjects::iterator end() { return m_Children.end(); }221 vector_pObjects::iterator begin(); 222 vector_pObjects::iterator end(); 206 223 207 224 //@} 208 225 //-------------------------------------------------------- … … 220 237 */ 221 238 bool SettingExists(const CStr& Setting) const; 222 239 240 std::map<CStr, SGUISetting>& GetSettings(); 241 const std::map<CStr, SGUISetting>& GetSettings() const; 242 SGUISetting& RefSetting(const CStr& name); 243 const SGUISetting& RefSetting(const CStr& name) const; 244 223 245 /** 246 * Add a setting to m_Settings 247 * 248 * @param Type Setting type 249 * @param Name Setting reference name 250 */ 251 void AddSetting(const EGUISettingType& Type, const CStr& Name); 252 253 /** 224 254 * All sizes are relative to resolution, and the calculation 225 255 * is not wanted in real time, therefore it is cached, update 226 256 * the cached size with this function. … … 277 307 //-------------------------------------------------------- 278 308 //@{ 279 309 280 /**281 * Add a setting to m_Settings282 *283 * @param Type Setting type284 * @param Name Setting reference name285 */286 void AddSetting(const EGUISettingType& Type, const CStr& Name);287 310 288 311 /** 289 312 * Calls Destroy on all children, and deallocates all memory. … … 291 314 */ 292 315 virtual void Destroy(); 293 316 294 public: 295 /** 296 * This function is called with different messages 297 * for instance when the mouse enters the object. 298 * 299 * @param Message GUI Message 317 /* 318 * Return children of this widget 300 319 */ 301 v irtual void HandleMessage(SGUIMessage& UNUSED(Message)) {}320 vector_pObjects& GetChildren(); 302 321 303 protected:304 322 /** 305 323 * Draws the object. 306 324 * … … 352 370 /** 353 371 * Set parent of this object 354 372 */ 355 void SetParent(IGUIObject* pParent) { m_pParent = pParent; }373 void SetParent(IGUIObject* pParent); 356 374 357 375 /** 358 376 * Reset internal state of this object 359 377 */ 360 virtual void ResetStates() 361 { 362 // Notify the gui that we aren't hovered anymore 363 UpdateMouseOver(NULL); 364 } 378 virtual void ResetStates(); 365 379 366 public:367 CGUI* GetGUI() { return m_pGUI; }368 const CGUI* GetGUI() const { return m_pGUI; }369 370 /**371 * Take focus!372 */373 void SetFocus();374 375 380 protected: 376 381 /** 377 382 * Check if object is focused. … … 443 448 444 449 void SetScriptHandler(const CStr& Action, JS::HandleObject Function); 445 450 451 double GetLastClickTime(int name) const; 452 void SetLastClickTime(int name, double time); 453 446 454 /** 455 * This is an array of true or false, each element is associated with 456 * a string representing a setting. Number of elements is equal to 457 * number of settings. 458 * 459 * A true means the setting has been manually set in the file when 460 * read. This is important to know because I don't want to force 461 * the user to include its \<styles\>-XML-files first, so somehow 462 * the GUI needs to know which settings were set, and which is meant 463 * to. 464 */ 465 466 // More variables 467 bool IsMouseHovering() const; 468 void SetMouseHovering(bool hover); 469 470 /** 447 471 * Inputes the object that is currently hovered, this function 448 472 * updates this object accordingly (i.e. if it's the object 449 473 * being inputted one thing happens, and not, another). … … 493 517 494 518 void TraceMember(JSTracer* trc); 495 519 496 // Variables520 JS::Heap<JSObject*>* GetJsEventHandler(const CStr& eventName); 497 521 498 protected: 499 // Name of object 500 CStr m_Name; 501 502 // Constructed on the heap, will be destroyed along with the the object 503 // TODO Gee: really the above? 504 vector_pObjects m_Children; 505 506 // Pointer to parent 507 IGUIObject *m_pParent; 508 509 //This represents the last click time for each mouse button 510 double m_LastClickTime[6]; 511 512 /** 513 * This is an array of true or false, each element is associated with 514 * a string representing a setting. Number of elements is equal to 515 * number of settings. 516 * 517 * A true means the setting has been manually set in the file when 518 * read. This is important to know because I don't want to force 519 * the user to include its \<styles\>-XML-files first, so somehow 520 * the GUI needs to know which settings were set, and which is meant 521 * to. 522 */ 523 524 // More variables 525 526 // Is mouse hovering the object? used with the function MouseOver() 527 bool m_MouseHovering; 528 529 /** 530 * Settings pool, all an object's settings are located here 531 * If a derived object has got more settings that the base 532 * settings, it's because they have a new version of the 533 * function SetupSettings(). 534 * 535 * @see SetupSettings() 536 */ 537 public: 538 std::map<CStr, SGUISetting> m_Settings; 539 540 private: 541 // An object can't function stand alone 542 CGUI *m_pGUI; 543 544 // Internal storage for registered script handlers. 545 std::map<CStr, JS::Heap<JSObject*> > m_ScriptHandlers; 546 547 // Cached JSObject representing this GUI object 548 DefPersistentRooted<JSObject*> m_JSObject; 522 std::unique_ptr<IGuiObjectImpl> m; 549 523 }; 550 524 551 525 -
gui/MiniMap.cpp
70 70 AddSetting(GUIST_CStrW, "tooltip"); 71 71 AddSetting(GUIST_CStr, "tooltip_style"); 72 72 m_Clicking = false; 73 m_MouseHovering = false;74 73 75 74 // Register Relax NG validator 76 75 CXeromyces::AddValidator(g_VFS, "pathfinder", "simulation/data/pathfinder.rng"); … … 143 142 switch (Message.type) 144 143 { 145 144 case GUIM_MOUSE_PRESS_LEFT: 146 if ( m_MouseHovering)145 if (IsMouseHovering()) 147 146 { 148 147 SetCameraPos(); 149 148 m_Clicking = true; … … 150 149 } 151 150 break; 152 151 case GUIM_MOUSE_RELEASE_LEFT: 153 if ( m_MouseHovering&& m_Clicking)152 if (IsMouseHovering() && m_Clicking) 154 153 SetCameraPos(); 155 154 m_Clicking = false; 156 155 break; 157 156 case GUIM_MOUSE_DBLCLICK_LEFT: 158 if ( m_MouseHovering&& m_Clicking)157 if (IsMouseHovering() && m_Clicking) 159 158 SetCameraPos(); 160 159 m_Clicking = false; 161 160 break; 162 161 case GUIM_MOUSE_ENTER: 163 m_MouseHovering = true;162 SetMouseHovering(true); 164 163 break; 165 164 case GUIM_MOUSE_LEAVE: 166 165 m_Clicking = false; 167 m_MouseHovering = false;166 SetMouseHovering(false); 168 167 break; 169 168 case GUIM_MOUSE_RELEASE_RIGHT: 170 169 CMiniMap::FireWorldClickEvent(SDL_BUTTON_RIGHT, 1); … … 173 172 CMiniMap::FireWorldClickEvent(SDL_BUTTON_RIGHT, 2); 174 173 break; 175 174 case GUIM_MOUSE_MOTION: 176 if ( m_MouseHovering&& m_Clicking)175 if (IsMouseHovering() && m_Clicking) 177 176 SetCameraPos(); 178 177 break; 179 178 case GUIM_MOUSE_WHEEL_DOWN: -
gui/scripting/JSInterface_IGUIObject.cpp
89 89 if (propName.substr(0, 2) == "on") 90 90 { 91 91 CStr eventName(CStr(propName.substr(2)).LowerCase()); 92 std::map<CStr, JS::Heap<JSObject*>>::iterator it = e->m_ScriptHandlers.find(eventName);93 if ( it == e->m_ScriptHandlers.end())92 auto* jsHeapObjectPtr = e->GetJsEventHandler(eventName); 93 if (jsHeapObjectPtr == nullptr) 94 94 vp.setNull(); 95 95 else 96 vp.setObject(* it->second.get());96 vp.setObject(*jsHeapObjectPtr->get()); 97 97 return true; 98 98 }