Iridescence 1.00
Embedded Graphic Framework
Loading...
Searching...
No Matches
ds_view.h
1/* Copyright (C) 2017 - 2024, Thornwave Labs Inc - All Rights Reserved.
2 * Unauthorized copying of this file, via any medium is strictly prohibited.
3 * Proprietary and confidential.
4 * Written by Razvan Turiac <razvan.turiac@thornwave.com>
5*/
6
7#ifndef _DS_VIEW_H
8#define _DS_VIEW_H
9
10
11#include <stdint.h>
12
13#include <ds_types.h>
14#include <ds_color.h>
15#include <ds_renderer.h>
16#include <ds_event.h>
17
18
19class DsScreen;
20
39class DsView
40{
41public:
47 {
48 public:
55 virtual void onViewStateChange(DsView* view, bool isVisible) = 0;
56 };
57
58
66 DsView(DsView* parent, const DsPoint &pos = DsPoint(), const DsSize &size = DsSize());
67
75 DsView(DsView* parent, DsOrientation layout_orientation);
76
77
83 void addChild(DsView* view);
84
90 void removeChild(DsView* view);
91
96
102 void bringFront(void);
103
109 void sendBack(void);
110
116 DsView* getParent(void) const;
117
124 void setParent(DsView* parent);
125
126
132 const DsPoint& getPosition(void) const;
133
134
141
142
151
152
160 DsPoint translatePositionInParentCoordinates(uint32_t parent_level, DsPoint pos) const;
161
162
170
171
177 void setPosition(const DsPoint &pos);
178
179
186 void setCenterPosition(const DsPoint &pos);
187
188
195
196
203
204
210 const DsSize& getParentSize(void) const;
211
212
218 const DsSize& getSize(void) const;
219
220
226 void setMinimumSize(const DsSize &size);
227
228
236 void setSize(const DsSize &size);
237
238
244 void setWidth(int32_t width);
245
246
252 void setHeight(int32_t height);
253
254
260 DsRect getRegion(void) const;
261
262
270
271
277 void alignInParent(void);
278
289 void alignInParent(const DsAlignment &alignment, int32_t padding_top, int32_t padding_left, int32_t padding_bottom, int32_t padding_right);
290
291
300 DsView* setPaddingTop(int16_t padding, uint16_t proportion = 0);
301
310 DsView* setPaddingBottom(int16_t padding, uint16_t proportion = 0);
311
320 DsView* setPaddingLeft(int32_t padding, uint16_t proportion = 0);
321
330 DsView* setPaddingRight(int16_t padding, uint16_t proportion = 0);
331
340 DsView* setPadding(int16_t padding, uint16_t proportion = 0);
341
342
351 DsView* setPaddingHorizontal(int16_t padding, uint16_t proportion = 0);
352
353
362 DsView* setPaddingVertical(int16_t padding, uint16_t proportion = 0);
363
364
396 DsView* setProportion(uint32_t proportion);
397
398
404
405
422
423
431 void reserveSpace(bool state);
432
433
440 void setAutoLayout(DsOrientation orientation, bool enable);
441
442
453 virtual DsSize fitContent(void);
454
455
461 void doLayout(void);
462
463
470
471
477 void setBackgroundImage(const DsSurface* surface);
478
479
486
487
493
494
499 void setPanelCornerRadius(uint16_t radius);
500
501
506 void setPanelBorderThickness(uint8_t thickness);
507
508
517
518
525 bool isFocused(void);
526
527
534 bool getIsVisible(void) const;
535
536
541 bool isVisible(void) const;
542
543
548 bool isParentVisible(void) const;
549
550
554 inline bool show(void)
555 {
556 return setVisible(true);
557 }
558
559
563 inline bool hide(void)
564 {
565 return setVisible(false);
566 }
567
568
577 bool setVisible(bool is_visible);
578
579
583 virtual void onShow(void);
584
585
589 virtual void onHide(void);
590
591
599 void enable(bool state);
600
601
606 bool isEnabled(void) const;
607
608
613 void enableTouch(bool state);
614
615
620 bool isTouchEnabled(void) const;
621
622
627 void sendTap(DsPoint pos);
628
633 void setOnTapCallback(const lambda<void(const DsPoint &p)> &cb);
634
635
640 void setOnLongTapCallback(const lambda<void(const DsPoint &p)> &cb);
641
642
647 void setOnTouchActiveCallback(const lambda<void(const DsPoint &p)> &cb);
648
649
654 void setOnTouchReleaseCallback(const lambda<void(void)> &cb);
655
656
661 void setLongTapDetection(uint32_t delay_ms);
662
663
668 bool isInputInside(void) const;
669
670
678 const DsPoint& getTouchPosition(void) const;
679
680
688 void enableHorizontalScroll(bool state);
689
690
698 void enableVerticalScroll(bool state);
699
700
709
710
716 virtual void invalidate(void);
717
718
725 virtual void invalidateRegion(DsRect region);
726
727
736
737
744
745
746protected:
747 DsPoint mScrollPosition;
748 DsPoint mPreviousScrollPosition;
749 DsPoint mScrollPositionBase;
750 DsColor mScrollbarColor;
751
752 DsPoint mTouchPos;
753 uint32_t mTouchTimestamp;
754
755
760 virtual void onChildAdd(DsView* child);
761
767 virtual void onSizeChange(void);
768
769
784 virtual void onPaint(DsRenderer &renderer);
785
786
800 virtual void onPaintOver(DsRenderer &renderer);
801
802
825 virtual bool onTouch(const DsTouchEvent &event);
826
832 virtual void onVSync(void);
833
834
840 virtual void onKeyboard(int32_t key);
841
842
849 void inputCapture(void);
850
855 bool isInputCaptured(void) const;
856
857
861 void skipInput(void);
862
863
871 void forAllChildren(const lambda<void(DsView* child)> &lambda);
872
873
881 void forAllChildrenBackwards(const lambda<void(DsView* child)> &lambda);
882
883
884private:
885 friend class DsScreen;
886 friend class DsList;
887 friend class DsGrid;
888 friend class DsBookView;
889
890 struct StateObserverLink
891 {
892 StateObserver* observer;
893 StateObserverLink* next;
894 };
895
896 DsView* mPrev;
897 DsView* mNext;
898 DsView* mChildrenHead;
899 DsView* mChildrenTail;
900
901 DsScreen* mScreen;
902 DsView* mParent;
903
904 DsPoint mPosition;
905 DsSize mMinimumSize;
906 DsSize mSize;
907
908 DsOrientation mLayoutOrientation;
909 uint32_t mLayoutTotalProportion;
910
911 int16_t mPaddingTop;
912 int16_t mPaddingBottom;
913 int16_t mPaddingLeft;
914 int16_t mPaddingRight;
915
916 uint16_t mPaddingProportionTop;
917 uint16_t mPaddingProportionBottom;
918 uint16_t mPaddingProportionLeft;
919 uint16_t mPaddingProportionRight;
920
921 uint32_t mProportion;
922 DsAlignment mParentAlignment;
923 DsSize mChildrenExtent;
924
925 DsColor mBackgroundColor;
926 const DsSurface* mBackgroundSurface;
927 DsColor mPanelColor;
928 DsColor mPanelBorderColor;
929 uint16_t mPanelCornerRadius;
930 uint8_t mPanelBorderThickness;
931
932 //used by the touch detector
933 lambda<void(const DsPoint&)>* mOnTapCallback;
934 lambda<void(const DsPoint&)>* mOnLongTapCallback;
935 lambda<void(const DsPoint&)>* mOnTouchActiveCallback;
936 lambda<void(void)>* mOnTouchReleaseCallback;
937
938 DsView* mInputCaptureView; //contains the view that captured the input
939 uint32_t mLongTapDelay;
940
941 StateObserverLink* mStateObservers;
942
943 struct
944 {
945 uint16_t mIsInputInside: 1;
946 uint16_t mSkipInput: 1;
947 uint16_t mIsTouchActive: 1;
948 uint16_t mIsLongTouchActive: 1;
949 uint16_t mIsScrollActive: 1;
950 uint16_t mIsTouchEnabled: 1;
951 uint16_t mIsVisible: 1;
952 uint16_t mIsEnabled: 1;
953 uint16_t mIsSpaceReserved: 1;
954 uint16_t mIsLayoutEnabled: 1;
955 uint16_t mIsHorizontalScrollEnabled: 1;
956 uint16_t mIsVerticalScrollEnabled: 1;
957 };
958
959
960 DsView(const DsView &rhs) = delete;
961 DsView& operator=(const DsView &rhs) = delete;
962
963 void bringFront(DsView* view);
964 void sendBack(DsView* view);
965
966 void inputRelease(void);
967 bool parentIsScrolling(void) const;
968
969 void setScreen(DsScreen* screen);
970 void onParentVisibilityChanged(bool is_visible);
971
972 void processPaintEvent(const DsSurface* parent_surface, DsRenderer &renderer);
973 bool processTouchEvent(DsTouchEvent event);
974 void processVSyncEvent(void);
975};
976
977
978#include <ds_screen.h>
979
980
981#endif
DsAlignment is a class representing both horizontal and vertical alignment type.
Definition ds_types.h:130
DsBookView is a class representing a container that arranges it's views as pages of a book.
Definition ds_book_view.h:20
DsColor is a class that represents a 4 channel color (Alpha, Red, Green, Blue).
Definition ds_color.h:53
DsFPoint is a class that represents the X and Y 2D coordinates of a point as floating point numbers.
Definition ds_geometry.h:397
Definition ds_grid.h:15
DsList is a class that implements a list that allows the user to view and select a specifc entry.
Definition ds_list.h:26
DsOrientation is a class representing the orientation (horizontal or vertical).
Definition ds_types.h:57
DsPoint is a class that represents the X and Y 2D coordinates of a point as integer numbers.
Definition ds_geometry.h:24
DsRect is a class that represents a 2D rectangle (X, Y, W, H)
Definition ds_geometry.h:1507
DsRenderer is a class that implements all the drawing operations.
Definition ds_renderer.h:42
DsScreen is class of type DsView (inherits from DsView) that implements a physical screen.
Definition ds_screen.h:29
DsSize is class that represents the 2D size of a graphic object (width, height) as integer values.
Definition ds_geometry.h:780
DsSurface is a class that represents a drawing surface.
Definition ds_surface.h:30
StateObserver is a pure virtual class implementing the interface for notifying state observers of cha...
Definition ds_view.h:47
virtual void onViewStateChange(DsView *view, bool isVisible)=0
Called when the visibility of the DsView changes.
DsView is a base class that represents any view on the screen.
Definition ds_view.h:40
DsFPoint getCenterPoint(void) const
Returns the center point of the view in current coordinates.
void setPanelCornerRadius(uint16_t radius)
Sets the radius of the panel corners.
void bringFront(void)
Brings the current view to the top of the drawing order.
DsView(DsView *parent, const DsPoint &pos=DsPoint(), const DsSize &size=DsSize())
Creates a DsView with an optional position and size.
void setPanelBorderThickness(uint8_t thickness)
Sets the thickness of the panel border.
void forAllChildrenBackwards(const lambda< void(DsView *child)> &lambda)
Calls the supplied lambda for every view in the children list.
DsView * setPaddingRight(int16_t padding, uint16_t proportion=0)
Sets the right padding used by the auto-layout.
void setPosition(const DsPoint &pos)
Sets the position of the view in parent coordinates.
void sendTap(DsPoint pos)
Sends a tap event to the view.
bool setVisible(bool is_visible)
Changes the visibility of the view.
void enable(bool state)
Enables or disables the view.
virtual void onSizeChange(void)
This virtual method will be called when the size of the view changes.
void doLayout(void)
Runs the auto-layout algorithm.
DsRect getRegionInParentCoordinates(const DsView *parent) const
Returns a rectangle describing the space occupied by the current view in the parent's coordinate syst...
void removeChild(DsView *view)
Removes a child to the current DsView.
virtual void invalidate(void)
Invalidates the view.
virtual void onVSync(void)
This virtual method is be called at every vertical sync period.
bool isVisible(void) const
Is the view actually visible on the screen?
void setSize(const DsSize &size)
Sets the size of the view.
void setOnTapCallback(const lambda< void(const DsPoint &p)> &cb)
Sets the lambda to be executed when the user taps on the view.
DsView * setParentAlignment(const DsAlignment &alignment)
Sets parent alignment mode of the view.
void alignInParent(const DsAlignment &alignment, int32_t padding_top, int32_t padding_left, int32_t padding_bottom, int32_t padding_right)
Aligns the view relative to it's parent.
void setOnLongTapCallback(const lambda< void(const DsPoint &p)> &cb)
Sets the lambda to be executed when the user taps the view and holds for a specific time (set with se...
virtual void onPaint(DsRenderer &renderer)
This virtual method will be called when the view needs to repaint it's content.
DsView * setPaddingHorizontal(int16_t padding, uint16_t proportion=0)
Sets the horizontal padding used by the auto-layout.
bool isParentVisible(void) const
Is the parent view actually visible on the screen?
DsScreen & getScreen(void)
Returns a reference to the screen to which this view belongs.
bool isEnabled(void) const
Is the view enabled?
void setMinimumSize(const DsSize &size)
Returns the minimum size of the view.
bool isFocused(void)
Is the view focused?
DsView * getParent(void) const
Returns the parent of the current view.
void reserveSpace(bool state)
Configures if during auto-layout the view will reserve space even if it's hidden.
virtual void onShow(void)
This virtual method is called when the view becomes visible on the screen.
virtual void onChildAdd(DsView *child)
This virtual method will be called when a new child had been added to the view.
void setHeight(int32_t height)
Sets the height of the view.
void centerVertically(DsView *view)
Aligns vertically the center of the current view with the center of another view.
void removeAllChildren(void)
Removes all children of the current DsView.
bool isInputCaptured(void) const
Is the input captured?
void alignInParent(void)
Aligns the view relative to it's parent.
DsAlignment getParentAlignment(void) const
Returns parent alignment mode of the view.
void addChild(DsView *view)
Adds a child to the current DsView.
virtual void onKeyboard(int32_t key)
This virtual method is called when there is a keyboard event that needs to be processed.
void enableHorizontalScroll(bool state)
Enables or disables the horizontal scroll of the view contents.
void removeStateObserver(StateObserver *observer)
Removes a state observer object.
virtual void onPaintOver(DsRenderer &renderer)
This virtual method will be called when the view needs to repaint it's content, but after the view an...
DsView(DsView *parent, DsOrientation layout_orientation)
Creates a DsView with the auto-layout mode active. This view can align it's children automatically.
bool isTouchEnabled(void) const
Is the view enabled to process touch events?
void centerHorizontally(DsView *view)
Aligns horizontally the center of the current view with the center of another view.
DsPoint translatePositionInParentCoordinates(const DsView *parent, DsPoint pos) const
Translates position (x, y) in parent coordinate system.
DsRect getRegion(void) const
Returns a rectangle describing the space occupied by the current view in it's own coordinate system (...
bool show(void)
Shortcut to setVisible(true).
Definition ds_view.h:554
void setOnTouchReleaseCallback(const lambda< void(void)> &cb)
Sets the lambda to be executed when the user touch is released.
virtual void onHide(void)
This virtual method is called when the view becomes invisible (hidden) on the screen.
virtual DsSize fitContent(void)
Calculates the minimum size required by the view.
virtual bool onTouch(const DsTouchEvent &event)
This virtual method will be called when there is touch user input.
bool hide(void)
Shortcut to setVisible(false).
Definition ds_view.h:563
DsPoint translatePositionInParentCoordinates(uint32_t parent_level, DsPoint pos) const
Translates position (x, y) in parent coordinate system.
void enableVerticalScroll(bool state)
Enables or disables the vertical scroll of the view contents.
bool getIsVisible(void) const
Is the view visibility enabled?
void setLongTapDetection(uint32_t delay_ms)
Configures the long tap detection.
DsView * setPaddingVertical(int16_t padding, uint16_t proportion=0)
Sets the vertical padding used by the auto-layout.
void enableTouch(bool state)
Enables or disables processing of touch events.
void setBackgroundImage(const DsSurface *surface)
Sets the background image.
virtual void invalidateRegion(DsRect region)
Invalidates only a region of the view.
void setBackgroundColor(DsColor color)
Sets the background color of the view.
DsView * setPaddingTop(int16_t padding, uint16_t proportion=0)
Sets the top padding used by the auto-layout.
bool isInputInside(void) const
Returns if there is user input (touch) inside the view.
DsView * setPaddingLeft(int32_t padding, uint16_t proportion=0)
Sets the left padding used by the auto-layout.
void addStateObserver(StateObserver *observer)
Adds a state observer object.
void setCenterPosition(const DsPoint &pos)
Sets the position of the center of the view in parent coordinates.
const DsSize & getParentSize(void) const
Returns the size of the parent view.
void setPanelBorderColor(DsColor color)
Sets the color of the panel border.
void setAutoLayout(DsOrientation orientation, bool enable)
Enables or disables the auto-layout mode and sets the primary orientation.
void setParent(DsView *parent)
Sets a new parent to the view.
void setOnTouchActiveCallback(const lambda< void(const DsPoint &p)> &cb)
Sets the lambda to be executed when the user touches the view.
const DsPoint & getPosition(void) const
Returns the position of the view in parent coordinates.
DsView * setPadding(int16_t padding, uint16_t proportion=0)
Sets the padding used by the auto-layout.
void setWidth(int32_t width)
Sets the width of the view.
DsView * setPaddingBottom(int16_t padding, uint16_t proportion=0)
Sets the bottom padding used by the auto-layout.
DsView * setProportion(uint32_t proportion)
Sets the view proportion used by the auto-layout.
DsPoint getPositionInParentCoordinates(const DsView *parent) const
Returns the position in the parent's coordinate system.
void skipInput(void)
Skips receiving touch inputs until the end of the current touch event.
void forAllChildren(const lambda< void(DsView *child)> &lambda)
Calls the supplied lambda for every view in the children list.
void sendBack(void)
Sends the current view to the bottom of the drawing order.
void inputCapture(void)
Captures the touch input.
void setScrollbarColor(DsColor color)
Configure the color of the scroll bars.
const DsSize & getSize(void) const
Returns the size of the view.
const DsPoint & getTouchPosition(void) const
Returns the location of the user touch input inside the view.
void setPanelColor(DsColor color)
Sets the color of the panel.
Definition ds_types_lambda.h:24
This structure implements a touch event.
Definition ds_event.h:25