xref: /haiku/src/libs/icon/shape/PathSourceShape.cpp (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
1 /*
2  * Copyright 2006, 2023, Haiku. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  *		Zardshard
8  */
9 
10 #include "PathSourceShape.h"
11 
12 #include <new>
13 
14 #include <agg_bounding_rect.h>
15 #include <Message.h>
16 
17 #ifdef ICON_O_MATIC
18 # include "CommonPropertyIDs.h"
19 # include "Property.h"
20 # include "PropertyObject.h"
21 #endif // ICON_O_MATIC
22 #include "Style.h"
23 
24 using std::nothrow;
25 
26 _USING_ICON_NAMESPACE
27 
28 
29 PathSourceShape::PathSourceShape(::Style* style)
30 	: Shape(style),
31 
32 	  fMinVisibilityScale(0.0),
33 	  fMaxVisibilityScale(4.0)
34 {
35 }
36 
37 
38 PathSourceShape::PathSourceShape(const PathSourceShape& other)
39 	: Shape(other),
40 
41 	  fMinVisibilityScale(other.fMinVisibilityScale),
42 	  fMaxVisibilityScale(other.fMaxVisibilityScale)
43 {
44 }
45 
46 
47 PathSourceShape::~PathSourceShape()
48 {
49 }
50 
51 
52 // #pragma mark -
53 
54 
55 status_t
56 PathSourceShape::Unarchive(BMessage* archive)
57 {
58 	Shape::Unarchive(archive);
59 
60 	// min visibility scale
61 	if (archive->FindFloat("min visibility scale", &fMinVisibilityScale) < B_OK)
62 		fMinVisibilityScale = 0.0;
63 
64 	// max visibility scale
65 	if (archive->FindFloat("max visibility scale", &fMaxVisibilityScale) < B_OK)
66 		fMaxVisibilityScale = 4.0;
67 
68 	if (fMinVisibilityScale < 0.0)
69 		fMinVisibilityScale = 0.0;
70 	if (fMinVisibilityScale > 4.0)
71 		fMinVisibilityScale = 4.0;
72 	if (fMaxVisibilityScale < 0.0)
73 		fMaxVisibilityScale = 0.0;
74 	if (fMaxVisibilityScale > 4.0)
75 		fMaxVisibilityScale = 4.0;
76 
77 	return B_OK;
78 }
79 
80 
81 #ifdef ICON_O_MATIC
82 status_t
83 PathSourceShape::Archive(BMessage* into, bool deep) const
84 {
85 	status_t ret = Shape::Archive(into, deep);
86 
87 	// min visibility scale
88 	if (ret == B_OK)
89 		ret = into->AddFloat("min visibility scale", fMinVisibilityScale);
90 
91 	// max visibility scale
92 	if (ret == B_OK)
93 		ret = into->AddFloat("max visibility scale", fMaxVisibilityScale);
94 
95 	return ret;
96 }
97 
98 
99 PropertyObject*
100 PathSourceShape::MakePropertyObject() const
101 {
102 	PropertyObject* object = Shape::MakePropertyObject();
103 	if (object == NULL)
104 		return NULL;
105 
106 //	object->AddProperty(new BoolProperty(PROPERTY_HINTING, fHinting));
107 
108 	object->AddProperty(new FloatProperty(PROPERTY_MIN_VISIBILITY_SCALE,
109 		fMinVisibilityScale, 0, 4));
110 
111 	object->AddProperty(new FloatProperty(PROPERTY_MAX_VISIBILITY_SCALE,
112 		fMaxVisibilityScale, 0, 4));
113 
114 	return object;
115 }
116 
117 
118 bool
119 PathSourceShape::SetToPropertyObject(const PropertyObject* object)
120 {
121 	AutoNotificationSuspender _(this);
122 	Shape::SetToPropertyObject(object);
123 
124 	// hinting
125 //	SetHinting(object->Value(PROPERTY_HINTING, fHinting));
126 
127 	// min visibility scale
128 	SetMinVisibilityScale(object->Value(PROPERTY_MIN_VISIBILITY_SCALE, fMinVisibilityScale));
129 
130 	// max visibility scale
131 	SetMaxVisibilityScale(object->Value(PROPERTY_MAX_VISIBILITY_SCALE, fMaxVisibilityScale));
132 
133 	return HasPendingNotifications();
134 }
135 #endif // ICON_O_MATIC
136 
137 
138 // #pragma mark -
139 
140 
141 status_t
142 PathSourceShape::InitCheck() const
143 {
144 	return Shape::InitCheck();
145 }
146 
147 
148 // #pragma mark -
149 
150 
151 void
152 PathSourceShape::SetMinVisibilityScale(float scale)
153 {
154 	if (fMinVisibilityScale == scale)
155 		return;
156 
157 	fMinVisibilityScale = scale;
158 	Notify();
159 }
160 
161 
162 void
163 PathSourceShape::SetMaxVisibilityScale(float scale)
164 {
165 	if (fMaxVisibilityScale == scale)
166 		return;
167 
168 	fMaxVisibilityScale = scale;
169 	Notify();
170 }
171 
172 
173 bool
174 PathSourceShape::Visible(float scale) const
175 {
176 	return scale >= MinVisibilityScale()
177 		&& (scale <= MaxVisibilityScale() || MaxVisibilityScale() >= 4.0f);
178 }
179 
180