style_get_value.h Source File - ClanLib SDK
Description: style_get_value.h Source File

style_get_value.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 The ClanLib Team
4**
5** This software is provided 'as-is', without any express or implied
6** warranty. In no event will the authors be held liable for any damages
7** arising from the use of this software.
8**
9** Permission is granted to anyone to use this software for any purpose,
10** including commercial applications, and to alter it and redistribute it
11** freely, subject to the following restrictions:
12**
13** 1. The origin of this software must not be misrepresented; you must not
14** claim that you wrote the original software. If you use this software
15** in a product, an acknowledgment in the product documentation would be
16** appreciated but is not required.
17** 2. Altered source versions must be plainly marked as such, and must not be
18** misrepresented as being the original software.
19** 3. This notice may not be removed or altered from any source distribution.
20**
21** Note: Some of the libraries ClanLib may link to may have additional
22** requirements or restrictions.
23**
24** File Author(s):
25**
26** Magnus Norddahl
27*/
28
29#pragma once
30
31#include <string>
32#include "../../Display/2D/color.h"
33#include "style_value_type.h"
34#include "style_dimension.h"
35
36namespace clan
37{
39 class StyleGetValue
40 {
41 public:
43 StyleValueType type() const { return _type; }
44
46 const char *text() const
47 {
48 switch (_type)
49 {
50 case StyleValueType::keyword:
51 case StyleValueType::url:
52 case StyleValueType::string:
53 return _value.text;
54 default:
55 return "";
56 }
57 }
58
60 float number() const
61 {
62 switch (_type)
63 {
64 case StyleValueType::length:
65 case StyleValueType::percentage:
66 case StyleValueType::number:
67 case StyleValueType::angle:
68 case StyleValueType::time:
69 case StyleValueType::frequency:
70 case StyleValueType::resolution:
71 return _value.float_value.number;
72 default:
73 return 0.0f;
74 }
75 }
76
78 StyleDimension dimension() const
79 {
80 switch (_type)
81 {
82 case StyleValueType::length:
83 case StyleValueType::angle:
84 case StyleValueType::time:
85 case StyleValueType::frequency:
86 case StyleValueType::resolution:
87 return _value.float_value.dimension;
88 default:
89 return StyleDimension::px;
90 }
91 }
92
94 Colorf color() const
95 {
96 if (_type == StyleValueType::color)
97 return Colorf(_value.color[0], _value.color[1], _value.color[2], _value.color[3]);
98 else
99 return Colorf();
100 }
101
103 bool is_undefined() const { return _type == StyleValueType::undefined; }
104
106 bool is_keyword() const { return _type == StyleValueType::keyword; }
107 bool is_keyword(const char *keyword) const { return is_keyword() && strcmp(_value.text, keyword) == 0; }
108 bool is_keyword(const std::string &keyword) const { return is_keyword() && _value.text == keyword; }
109
111 bool is_length() const { return _type == StyleValueType::length; }
112
114 bool is_angle() const { return _type == StyleValueType::angle; }
115
117 bool is_time() const { return _type == StyleValueType::time; }
118
120 bool is_frequency() const { return _type == StyleValueType::frequency; }
121
123 bool is_resolution() const { return _type == StyleValueType::resolution; }
124
126 bool is_percentage() const { return _type == StyleValueType::percentage; }
127
129 bool is_number() const { return _type == StyleValueType::number; }
130
132 bool is_string() const { return _type == StyleValueType::string; }
133
135 bool is_url() const { return _type == StyleValueType::url; }
136
138 bool is_color() const { return _type == StyleValueType::color; }
139
141 static StyleGetValue from_keyword(const char *keyword) { StyleGetValue v; v._type = StyleValueType::keyword; v._value.text = keyword; return v; }
142
144 static StyleGetValue from_string(const char *text) { StyleGetValue v; v._type = StyleValueType::string; v._value.text = text; return v; }
145
147 static StyleGetValue from_length(float length, StyleDimension dimension = StyleDimension::px) { StyleGetValue v; v._type = StyleValueType::length; v._value.float_value.number = length; v._value.float_value.dimension = dimension; return v; }
148
150 static StyleGetValue from_angle(float angle, StyleDimension dimension = StyleDimension::rad) { StyleGetValue v; v._type = StyleValueType::angle; v._value.float_value.number = angle; v._value.float_value.dimension = dimension; return v; }
151
153 static StyleGetValue from_time(float t, StyleDimension dimension = StyleDimension::s) { StyleGetValue v; v._type = StyleValueType::time; v._value.float_value.number = t; v._value.float_value.dimension = dimension; return v; }
154
156 static StyleGetValue from_frequency(float freq, StyleDimension dimension = StyleDimension::hz) { StyleGetValue v; v._type = StyleValueType::frequency; v._value.float_value.number = freq; v._value.float_value.dimension = dimension; return v; }
157
159 static StyleGetValue from_resolution(float resolution, StyleDimension dimension = StyleDimension::dppx) { StyleGetValue v; v._type = StyleValueType::resolution; v._value.float_value.number = resolution; v._value.float_value.dimension = dimension; return v; }
160
162 static StyleGetValue from_percentage(float percentage) { StyleGetValue v; v._type = StyleValueType::percentage; v._value.float_value.number = percentage; return v; }
163
165 static StyleGetValue from_number(float number) { StyleGetValue v; v._type = StyleValueType::number; v._value.float_value.number = number; return v; }
166
168 static StyleGetValue from_url(const char *url) { StyleGetValue v; v._type = StyleValueType::url; v._value.text = url; return v; }
169
171 static StyleGetValue from_color(const Colorf &color) { StyleGetValue v; v._type = StyleValueType::color; v._value.color[0] = color.r; v._value.color[1] = color.g; v._value.color[2] = color.b; v._value.color[3] = color.a; return v; }
172
173 private:
174 StyleValueType _type = StyleValueType::undefined;
175
176 union
177 {
178 const char *text;
179 struct
180 {
181 float number;
182 StyleDimension dimension;
183 } float_value;
184 float color[4];
185 } _value;
186 };
187}
clan::Colorf
Floating point color description class (for float).
Definition color.h:799
clan::StyleGetValue
Style value returned by style classes.
Definition style_get_value.h:40
clan::StyleGetValue::color
Colorf color() const
Value color.
Definition style_get_value.h:94
clan::StyleGetValue::is_undefined
bool is_undefined() const
Check if value is undefined.
Definition style_get_value.h:103
clan::StyleGetValue::from_number
static StyleGetValue from_number(float number)
Create style value from a number.
Definition style_get_value.h:165
clan::StyleGetValue::from_color
static StyleGetValue from_color(const Colorf &color)
Create style value from a color.
Definition style_get_value.h:171
clan::StyleGetValue::from_url
static StyleGetValue from_url(const char *url)
Create style value from an url.
Definition style_get_value.h:168
clan::StyleGetValue::dimension
StyleDimension dimension
Definition style_get_value.h:182
clan::StyleGetValue::from_frequency
static StyleGetValue from_frequency(float freq, StyleDimension dimension=StyleDimension::hz)
Create style value from a frequency.
Definition style_get_value.h:156
clan::StyleGetValue::text
const char * text() const
Text when the type is a text string.
Definition style_get_value.h:46
clan::StyleGetValue::is_url
bool is_url() const
Check if value is an url.
Definition style_get_value.h:135
clan::StyleGetValue::is_keyword
bool is_keyword(const std::string &keyword) const
Definition style_get_value.h:108
clan::StyleGetValue::from_angle
static StyleGetValue from_angle(float angle, StyleDimension dimension=StyleDimension::rad)
Create style value from an angle.
Definition style_get_value.h:150
clan::StyleGetValue::from_resolution
static StyleGetValue from_resolution(float resolution, StyleDimension dimension=StyleDimension::dppx)
Create style value from a resolution.
Definition style_get_value.h:159
clan::StyleGetValue::is_length
bool is_length() const
Check if value is a length.
Definition style_get_value.h:111
clan::StyleGetValue::from_time
static StyleGetValue from_time(float t, StyleDimension dimension=StyleDimension::s)
Create style value from a time.
Definition style_get_value.h:153
clan::StyleGetValue::is_keyword
bool is_keyword(const char *keyword) const
Definition style_get_value.h:107
clan::StyleGetValue::from_string
static StyleGetValue from_string(const char *text)
Create style value from a string.
Definition style_get_value.h:144
clan::StyleGetValue::float_value
struct clan::StyleGetValue::@012201010135270116132364132003361130053164077361::@225117121227036366312236337045023336204300222327 float_value
clan::StyleGetValue::is_keyword
bool is_keyword() const
Check if value is a keyword.
Definition style_get_value.h:106
clan::StyleGetValue::is_resolution
bool is_resolution() const
Check if value is a resolution.
Definition style_get_value.h:123
clan::StyleGetValue::dimension
StyleDimension dimension() const
Dimension used by value.
Definition style_get_value.h:78
clan::StyleGetValue::type
StyleValueType type() const
Variant type.
Definition style_get_value.h:43
clan::StyleGetValue::from_percentage
static StyleGetValue from_percentage(float percentage)
Create style value from a percentage.
Definition style_get_value.h:162
clan::StyleGetValue::is_color
bool is_color() const
Check if value is a color.
Definition style_get_value.h:138
clan::StyleGetValue::number
float number() const
Value number.
Definition style_get_value.h:60
clan::StyleGetValue::from_length
static StyleGetValue from_length(float length, StyleDimension dimension=StyleDimension::px)
Create style value from a length.
Definition style_get_value.h:147
clan::StyleGetValue::is_time
bool is_time() const
Check if value is a time.
Definition style_get_value.h:117
clan::StyleGetValue::from_keyword
static StyleGetValue from_keyword(const char *keyword)
Create style value from a keyword.
Definition style_get_value.h:141
clan::StyleGetValue::number
float number
Definition style_get_value.h:181
clan::StyleGetValue::is_percentage
bool is_percentage() const
Check if value is a percentage.
Definition style_get_value.h:126
clan::StyleGetValue::is_string
bool is_string() const
Check if value is a string.
Definition style_get_value.h:132
clan::StyleGetValue::color
float color[4]
Definition style_get_value.h:184
clan::StyleGetValue::text
const char * text
Definition style_get_value.h:178
clan::StyleGetValue::is_angle
bool is_angle() const
Check if value is an angle.
Definition style_get_value.h:114
clan::StyleGetValue::is_frequency
bool is_frequency() const
Check if value is a frequency.
Definition style_get_value.h:120
clan::StyleGetValue::is_number
bool is_number() const
Check if value is a number.
Definition style_get_value.h:129
clan
Definition clanapp.h:36
clan::StyleValueType
StyleValueType
Style value type.
Definition style_value_type.h:35
clan::StyleValueType::time
@ time
value is an angle
Definition style_value_type.h:45
clan::StyleValueType::length
@ length
value is a keyword
Definition style_value_type.h:38
clan::StyleValueType::url
@ url
value is a text string
Definition style_value_type.h:42
clan::StyleValueType::undefined
@ undefined
Definition style_value_type.h:36
clan::StyleValueType::color
@ color
value is an url
Definition style_value_type.h:43
clan::StyleValueType::angle
@ angle
value is a color
Definition style_value_type.h:44
clan::StyleValueType::number
@ number
value is a percentage number
Definition style_value_type.h:40
clan::StyleValueType::string
@ string
value is a number
Definition style_value_type.h:41
clan::StyleValueType::resolution
@ resolution
value is a frequency
Definition style_value_type.h:47
clan::StyleValueType::percentage
@ percentage
value is a length
Definition style_value_type.h:39
clan::StyleValueType::keyword
@ keyword
value undefined
Definition style_value_type.h:37
clan::StyleValueType::frequency
@ frequency
value is a time
Definition style_value_type.h:46
clan::Key::v
@ v
Definition keys.h:102
clan::Key::t
@ t
Definition keys.h:100
clan::StyleDimension
StyleDimension
Unit of a style value.
Definition style_dimension.h:35
clan::StyleDimension::s
@ s
turns (1 in a full circle)
Definition style_dimension.h:54
clan::StyleDimension::hz
@ hz
milliseconds
Definition style_dimension.h:56
clan::StyleDimension::px
@ px
Definition style_dimension.h:36
clan::StyleDimension::rad
@ rad
gradians/gons/grades (400 in a full circle)
Definition style_dimension.h:52
clan::StyleDimension::dppx
@ dppx
dots per cm
Definition style_dimension.h:60
clan::StyleTokenType::percentage
@ percentage
Number.
Definition style_token.h:45