This repository has been archived on 2025-03-20. You can view files and clone it, but cannot push or open issues or pull requests.
The-Powder-Toy/src/gui/interface/Component.h
Tamás Bálint Misius 69e0a8b0aa
Clean up OnMouseClick/Unclick madness
This is a prerequisite for making ScrollPanel work nicely on touch screens.

Engine used the terms MouseClick and MouseUnclick to refer to events that are traditionally called MouseDown and MouseUp, this was fixed with simple renaming.

Component and friends similarly used the terms MouseClick and MouseUnclick to refer to events that are traditionally called MouseDown and MouseUp and, succumbing to their own confusing terminology, also implemented behaviours associated with both the actual events MouseDown and MouseClick in code that was responsible for handling only the actual event MouseDown (i.e. what they called MouseClick).

This had been overlooked for a long time because nobody cares that a checkbox changes state when the mouse button is pressed on it rather than when it is released.

The fix is to migrate many pieces of code that run in response to MouseDown events, such as checkbox state change code, to MouseClick events, and to redefine a MouseClick to mean a sequence of MouseDown and MouseUp inside the component, rather than just a MouseDown. This is complicated by the fact that MouseClick events report mouse coordinates relative to the top left corner of the component, while MouseDown events report them relative to the top left corner of the container of the component.

Other pieces of code that make sense to be run in response to MouseDown events, such as label selection code, were left alone.
2024-02-06 13:25:53 +01:00

204 lines
4.9 KiB
C++

#pragma once
#include "common/String.h"
#include "Appearance.h"
#include "Point.h"
class Graphics;
namespace ui
{
class ContextMenu;
class Window;
class Panel;
/* class Component
*
* An interactive UI component that can be added to a state or an XComponent*.
* *See sys::XComponent
*/
class Component
{
private:
Window* parentstate_;
Panel* _parent;
protected:
bool drawn;
ui::Point textPosition;
ui::Point textSize;
ui::Point iconPosition;
ui::ContextMenu * menu;
Graphics * GetGraphics();
public:
Component(Point position, Point size);
virtual ~Component();
void* UserData;
inline Window* const GetParentWindow() const { return parentstate_; }
bool IsFocused() const;
void Invalidate() { drawn = false; }
Point Position;
Point Size;
bool Enabled;
bool Visible;
bool DoesTextInput;
bool MouseDownInside;
ui::Appearance Appearance;
//virtual void SetAppearance(ui::Appearance);
//ui::Appearance GetAppearance();
virtual void TextPosition(String);
void Refresh();
Point GetContainerPos();
Point GetScreenPos();
/* See the parent of this component.
* If new_parent is NULL, this component will have no parent. (THIS DOES NOT delete THE COMPONENT. See XComponent::RemoveChild)
*/
void SetParentWindow(Window* window);
void SetParent(Panel* new_parent);
//Get the parent component.
inline Panel* const GetParent() const { return _parent; }
virtual void OnContextMenuAction(int item);
///
// Called: Every tick.
// Params:
// dt: The change in time.
///
virtual void Tick(float dt);
///
// Called: When ready to draw.
// Params:
// None
///
virtual void Draw(const Point& screenPos);
///
// Called: When the mouse is currently hovering over the item. (Called every tick)
// Params:
// localx: Local mouse X position.
// localy: Local mouse Y position.
///
virtual void OnMouseHover(int localx, int localy);
///
// Called: When the mouse moves.
// Params:
// localx: Local mouse X position.
// localy: Local mouse Y position.
// dx: Mouse X delta.
// dy: Mouse Y delta.
///
virtual void OnMouseMoved(int localx, int localy, int dx, int dy);
///
// Called: When the mouse moves.
// Params:
// localx: Local mouse X position.
// localy: Local mouse Y position.
// dx: Mouse X delta.
// dy: Mouse Y delta.
///
virtual void OnMouseMovedInside(int localx, int localy, int dx, int dy);
///
// Called: When the mouse moves on top of the item.
// Params:
// localx: Local mouse X position.
// localy: Local mouse Y position.
// dx: Mouse X delta.
// dy: Mouse Y delta.
///
virtual void OnMouseEnter(int localx, int localy);
///
// Called: When the mouse leaves the item.
// Params:
// localx: Local mouse X position.
// localy: Local mouse Y position.
///
virtual void OnMouseLeave(int localx, int localy);
///
// Called: When a mouse button is pressed.
// Params:
// x: X position of the mouse.
// y: Y position of the mouse.
// button: The button that is being held down.
///
virtual void OnMouseDown(int x, int y, unsigned button);
///
// Called: When a mouse button is released.
// Params:
// x: X position of the mouse.
// y: Y position of the mouse.
// button: The button that is being released.
///
virtual void OnMouseUp(int x, int y, unsigned button);
///
// Called: When a mouse button is pressed and then released on top of the item.
// Params:
// x: X position of the mouse.
// y: Y position of the mouse.
// button: The button that is being held down.
///
virtual void OnMouseClick(int localx, int localy, unsigned button);
///
// Called: When the mouse wheel moves/changes.
// Params:
// localx: Local mouse X position.
// localy: Local mouse Y position.
// d: The vertical scroll offset
///
virtual void OnMouseWheel(int localx, int localy, int d);
///
// Called: When the mouse wheel moves/changes on top of the item.
// Params:
// localx: Local mouse X position.
// localy: Local mouse Y position.
// d: The vertical scroll offset
///
virtual void OnMouseWheelInside(int localx, int localy, int d);
///
// Called: When a key is pressed.
// Params:
// key: The value of the key that is being pressed.
// shift: Shift key is down.
// ctrl: Control key is down.
// alt: Alternate key is down.
///
virtual void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
///
// Called: When a key is released.
// Params:
// key: The value of the key that is being released.
// shift: Shift key is released.
// ctrl: Control key is released.
// alt: Alternate key is released.
///
virtual void OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void OnTextInput(String text);
virtual void OnTextEditing(String text);
virtual void OnFocus();
virtual void OnDefocus();
};
}