Descent3/ui/UIHotspot.cpp

206 lines
4.8 KiB
C++
Raw Normal View History

/*
* Descent 3
* Copyright (C) 2024 Parallax Software
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
--- HISTORICAL COMMENTS FOLLOW ---
2024-04-16 03:43:29 +00:00
* $Logfile: /DescentIII/Main/ui/UIHotspot.cpp $
* $Revision: 16 $
* $Date: 4/26/99 9:11p $
* $Author: Matt $
*
* Hotspot code.
*
* $Log: /DescentIII/Main/ui/UIHotspot.cpp $
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 16 4/26/99 9:11p Matt
* Took out saturation used by old newui.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 15 4/24/99 8:23p Samir
* set hotspot hotkey after gadget is created.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 14 4/14/99 1:53a Jeff
* fixed case mismatched #includes
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 13 10/13/98 3:47p Samir
* hotspots now handle spacebats
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 12 10/01/98 12:57p Samir
* fixed hotspots so their width and height touch text, no outer border.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 11 9/30/98 4:33p Samir
* disabled gadgets display dim text.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 10 6/05/98 5:58p Jeff
* Added a way to change the items for the states of a HotSpot
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 9 5/01/98 6:23p Samir
* saturate hotspots when active.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 8 3/09/98 3:43p Samir
* Hotspot selected when mouse button goes up.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 7 1/30/98 7:05p Samir
* Each gadget now has their own coordinate system.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 6 1/23/98 5:34p Samir
* Allow for nonpersistant UIItems.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 5 1/18/98 4:22p Samir
* Implemented new UIItem system.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 4 1/05/98 4:36p Samir
* Moved centering and fittext control flags to UIGadget class.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 3 1/02/98 2:13p Samir
* Set alpha to full for item with focus.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 2 1/02/98 12:47p Samir
* Added selection and bettering centering?
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 1 12/30/97 4:36p Samir
* Initial revision
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* $NoKeywords: $
*/
#include "UIlib.h"
// ----------------------------------------------------------------------------
// UIHotspot
// This is simply a region within the parent window that when clicked on
// or key pressed, does something.
2024-04-16 18:56:40 +00:00
UIHotspot::UIHotspot() {
m_ItemOff = NULL;
m_ItemOn = NULL;
2024-04-16 03:43:29 +00:00
}
2024-04-16 18:56:40 +00:00
UIHotspot::~UIHotspot() {}
2024-04-16 03:43:29 +00:00
// optional key, when pressed, triggers hotspot.
2024-04-16 18:56:40 +00:00
void UIHotspot::Create(UIWindow *wnd, int id, int key, UIItem *itemoff, UIItem *itemon, int x, int y, int w, int h,
int flags) {
ASSERT(itemoff);
ASSERT(itemon);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
if (itemoff)
m_ItemOff = itemoff->CopyUIItem();
if (itemon)
m_ItemOn = itemon->CopyUIItem();
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
m_ItemCur = m_ItemOff;
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
UIGadget::Create(wnd, id, x, y, w, h, flags);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
if (key) {
UIGadget::SetHotkey(key);
}
2024-04-16 03:43:29 +00:00
}
2024-04-16 18:56:40 +00:00
void UIHotspot::SetStates(UIItem *itemoff, UIItem *itemon) {
ASSERT(itemoff);
ASSERT(itemon);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// determine which state it currently set, so we can set it on the new state
bool currstate = false;
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
if (m_ItemOn == m_ItemCur)
currstate = true;
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
if (m_ItemOff)
delete m_ItemOff;
if (m_ItemOn)
delete m_ItemOn;
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
m_ItemOff = itemoff->CopyUIItem();
m_ItemOn = itemon->CopyUIItem();
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
if (currstate)
m_ItemCur = m_ItemOn;
else
m_ItemCur = m_ItemOff;
2024-04-16 03:43:29 +00:00
}
// override: behavior when gadget is destroyed.
2024-04-16 18:56:40 +00:00
void UIHotspot::OnDestroy() {
UIGadget::OnDestroy();
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
if (m_ItemOff)
delete m_ItemOff;
if (m_ItemOn)
delete m_ItemOn;
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
m_ItemOff = NULL;
m_ItemOn = NULL;
2024-04-16 03:43:29 +00:00
}
// behavior when key is pressed.
2024-04-16 18:56:40 +00:00
void UIHotspot::OnKeyDown(int key) {
if (key == KEY_SPACEBAR) {
OnSelect();
}
2024-04-16 03:43:29 +00:00
}
// behavior when mouse button is pressed.
2024-04-16 18:56:40 +00:00
void UIHotspot::OnMouseBtnDown(int btn) {
if (btn == UILMSEBTN) {
LOCK_FOCUS(this);
}
2024-04-16 03:43:29 +00:00
}
// override: behavior when mouse button is released.
2024-04-16 18:56:40 +00:00
void UIHotspot::OnMouseBtnUp(int btn) {
if (btn == UILMSEBTN) {
if (HasFocus()) {
if (PT_IN_GADGET(UIGadget::m_Wnd, this, UI_input.mx, UI_input.my)) {
OnSelect();
}
}
UNLOCK_FOCUS(this);
}
2024-04-16 03:43:29 +00:00
}
// override: behavior when gadget loses input focus.
2024-04-16 18:56:40 +00:00
void UIHotspot::OnLostFocus() { m_ItemCur = m_ItemOff; }
2024-04-16 03:43:29 +00:00
// override: behavior when gadget gains input focus.
2024-04-16 18:56:40 +00:00
void UIHotspot::OnGainFocus() { m_ItemCur = m_ItemOn; }
2024-04-16 03:43:29 +00:00
// override: called when resized or before drawing.
2024-04-16 18:56:40 +00:00
void UIHotspot::OnFormat() {
if (m_Flags & UIF_FIT) {
m_W = m_ItemCur->width();
m_H = m_ItemCur->height();
}
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
UIGadget::OnFormat();
}
2024-04-16 03:43:29 +00:00
// behavior when gadget is being drawn.
2024-04-16 18:56:40 +00:00
void UIHotspot::OnDraw() {
// draw simple background.
if (m_ItemCur) {
int x = (m_W / 2 - m_ItemCur->width() / 2);
int y = (m_H / 2 - m_ItemCur->height() / 2);
if (IsDisabled())
m_ItemCur->draw(x, y, uiDrawFaded);
else {
m_ItemCur->draw(x, y);
}
}
2024-04-16 03:43:29 +00:00
}