@usman404/crowjs 1.0.4 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
- import {UIComponent} from './UIComponent.js';
1
+ import { TextComponent } from './TextComponent.js';
2
2
  import { Component } from '../Core/Component.js';
3
3
 
4
- export class Label extends UIComponent{
4
+ export class Label extends TextComponent{
5
5
  /**
6
6
  * Creates a text label component
7
7
  * @param {number} x - The x-coordinate
@@ -19,10 +19,10 @@ export class Label extends UIComponent{
19
19
  * @param {number} options.borderWidth - Border width
20
20
  * @param {number} options.cornerRadius - Corner radius
21
21
  * @param {boolean} options.enableShadow - Enable shadow rendering
22
- * @param {string} options.shadowColor - Shadow color
23
- * @param {number} options.shadowIntensity - Shadow opacity
24
- * @param {number} options.shadowSpread - Shadow spread
25
- * @param {number} options.shadowDetail - Shadow layers
22
+ * @param {string} options.shadowColor - Shadow color (CSS color string)
23
+ * @param {number} options.shadowBlur - Shadow blur radius
24
+ * @param {number} options.shadowOffsetX - Shadow offset on X axis
25
+ * @param {number} options.shadowOffsetY - Shadow offset on Y axis
26
26
  * @param {string} options.HTextAlign - Horizontal text alignment
27
27
  * @param {string} options.VTextAlign - Vertical text alignment
28
28
  * @param {number} options.pad - General padding
@@ -32,57 +32,114 @@ export class Label extends UIComponent{
32
32
  * @param {number} options.padr - Right padding
33
33
  * @param {number} options.padt - Top padding
34
34
  * @param {number} options.padb - Bottom padding
35
+ * @param {boolean} options.wrap - Whether to wrap text
36
+ * @param {string} options.wrapMode - Wrap mode: "word" or "char"
37
+ * @param {string} options.noWrapMode - No-wrap mode: "ellipsis" or "font-size"
38
+ * @param {string} options.ellipsisMode - Ellipsis mode: "leading", "center", or "trailing"
39
+ * @param {p5.Image|null} options.icon - Icon image (null = text only)
40
+ * @param {number} options.iconSize - Icon display size in pixels
41
+ * @param {string} options.iconPosition - "left", "right", "top", or "bottom"
42
+ * @param {number} options.iconGap - Gap between icon and text
43
+ * @param {p5.Color|null} options.iconTintColor - Optional icon tint
44
+ * @param {number} options.iconOpacity - Icon opacity 0-255
45
+ * @param {number} options.margin - General margin for all sides
46
+ * @param {number} options.marginx - Horizontal margin (left and right)
47
+ * @param {number} options.marginy - Vertical margin (top and bottom)
48
+ * @param {number} options.marginl - Left margin
49
+ * @param {number} options.marginr - Right margin
50
+ * @param {number} options.margint - Top margin
51
+ * @param {number} options.marginb - Bottom margin
35
52
  */
36
53
  constructor(x, y, width, height, label,
37
54
  {id=null,
38
55
  parent = null,
39
- backgroundColor = color(200),
40
- textColor = color(0),
56
+ backgroundColor = color('#1e1e2e'),
57
+ textColor = color('#e0e0e0'),
41
58
  borderFlag = true,
42
- borderColor = color(0),
59
+ borderColor = color('#3a3a4d'),
43
60
  borderWidth = 1,
44
- cornerRadius = 0,
61
+ cornerRadius = 8,
45
62
  enableShadow=false,
46
- shadowColor= 'rgb(0,0,0)',
47
- shadowIntensity= 0.4,
48
- shadowSpread= 3,
49
- shadowDetail=5,
63
+ shadowColor= 'rgba(0,0,0,0.5)',
64
+ shadowBlur= 12,
65
+ shadowOffsetX= 0,
66
+ shadowOffsetY= 4,
50
67
  HTextAlign="center",
51
68
  VTextAlign="center",
52
69
  pad = 5,
53
- padx = 0,
54
- pady = 0,
55
- padl = 0,
56
- padr = 0,
57
- padt = 0,
58
- padb = 0,
70
+ padx = null,
71
+ pady = null,
72
+ padl = null,
73
+ padr = null,
74
+ padt = null,
75
+ padb = null,
76
+ wrap = false,
77
+ wrapMode = "word",
78
+ noWrapMode = "font-size",
79
+ ellipsisMode = "trailing",
80
+ icon = null,
81
+ iconSize = 20,
82
+ iconPosition = 'left',
83
+ iconGap = 6,
84
+ iconTintColor = null,
85
+ iconOpacity = 255,
86
+ margin = 0,
87
+ marginx = null,
88
+ marginy = null,
89
+ marginl = null,
90
+ marginr = null,
91
+ margint = null,
92
+ marginb = null,
93
+ minWidth = 0,
94
+ minHeight = 0,
95
+ showDebugOverlay = false,
59
96
  } = {}) {
60
- super(x, y, width, height, backgroundColor, borderFlag, borderColor,
61
- borderWidth, cornerRadius, enableShadow, shadowColor, shadowIntensity,
62
- shadowSpread, shadowDetail, {parent: parent, type: "UIComponent", id: id});
63
-
64
- this.text = label;
65
- this.labelSize = 20;
66
- this.textColor = textColor;
67
-
68
- this.HTextAlign = HTextAlign;
69
- this.VTextAlign = VTextAlign;
70
-
71
- this.pad = pad;
72
- this.padx = padx;
73
- this.pady = pady;
74
- this.padl = padl;
75
- this.padr = padr;
76
- this.padt = padt;
77
- this.padb = padb;
97
+ super(x, y, width, height, label, {
98
+ id,
99
+ parent,
100
+ backgroundColor,
101
+ textColor,
102
+ borderFlag,
103
+ borderColor,
104
+ borderWidth,
105
+ cornerRadius,
106
+ enableShadow,
107
+ shadowColor,
108
+ shadowBlur,
109
+ shadowOffsetX,
110
+ shadowOffsetY,
111
+ HTextAlign,
112
+ VTextAlign,
113
+ pad,
114
+ padx,
115
+ pady,
116
+ padl,
117
+ padr,
118
+ padt,
119
+ padb,
120
+ wrap,
121
+ wrapMode,
122
+ noWrapMode,
123
+ ellipsisMode,
124
+ icon,
125
+ iconSize,
126
+ iconPosition,
127
+ iconGap,
128
+ iconTintColor,
129
+ iconOpacity,
130
+ margin,
131
+ marginx,
132
+ marginy,
133
+ marginl,
134
+ marginr,
135
+ margint,
136
+ marginb,
137
+ minWidth,
138
+ minHeight,
139
+ showDebugOverlay,
140
+ });
78
141
  }
79
142
 
80
- // if(this.enableShadow){
81
- // this.shadowColor = shadowColor;//rgb value
82
- // this.shadowIntensity = shadowIntensity;//opacity value between 0 and 1
83
- // this.shadowSpread = shadowSpread;//stroke width of each of those rectangles
84
- // this.shadowDetail = shadowDetail;//number of rectangles that will be drawn around the component
85
- // }
86
143
  /**
87
144
  * Renders the label with text, background, and border
88
145
  */
@@ -103,36 +160,7 @@ export class Label extends UIComponent{
103
160
 
104
161
  // Text
105
162
  fill(this.textColor);
106
- // textAlign(CENTER, CENTER);
107
- textSize(this.labelSize);
108
-
109
- let x;
110
- if(this.HTextAlign === "left"){
111
- textAlign(LEFT, CENTER);
112
- x = this.pad;
113
- } else if(this.HTextAlign === "right"){
114
- textAlign(RIGHT, CENTER);
115
- x = this.width - this.pad;
116
- } else{
117
- //center
118
- textAlign(CENTER, CENTER);
119
- x = this.width / 2;
120
- }
121
-
122
- let y;
123
- if(this.VTextAlign === "top"){
124
- textAlign(this.getHTextAlign(), BOTTOM);
125
- y = this.labelSize + this.pad;
126
- } else if(this.VTextAlign === "bottom"){
127
- textAlign(this.getHTextAlign(), TOP);
128
- y = this.height - this.labelSize - this.pad;
129
- } else {
130
- //center
131
- textAlign(this.getHTextAlign(), CENTER);
132
- y = this.height / 2;
133
- }
134
-
135
- text(this.text, x, y);
163
+ this.renderText();
136
164
  // rect(x, y, this.labelSize, this.labelSize);
137
165
 
138
166
  // Border
@@ -146,89 +174,5 @@ export class Label extends UIComponent{
146
174
  pop();
147
175
  }
148
176
 
149
- /**
150
- * Converts horizontal alignment string to P5 constant
151
- * @returns {number} P5 alignment constant
152
- */
153
- getHTextAlign(){
154
- switch(this.HTextAlign){
155
- case "left":
156
- return LEFT;
157
- case "right":
158
- return RIGHT;
159
- default:
160
- return CENTER;
161
- }
162
- }
163
-
164
- /**
165
- * Converts vertical alignment string to P5 constant
166
- * @returns {number} P5 alignment constant
167
- */
168
- getVTextAlign(){
169
- switch(this.VTextAlign){
170
- case "top":
171
- return TOP;
172
- case "bottom":
173
- return BOTTOM;
174
- default:
175
- return CENTER;
176
- }
177
- }
178
-
179
- /**
180
- * Updates the label text and recalculates text size
181
- * @param {string} text - The new text to display
182
- */
183
- setText(text){
184
- this.text = text;
185
- this.updateLabelSize();
186
- }
187
-
188
- //needs heavy optimizations
189
- /**
190
- * Dynamically calculates the optimal text size to fit the container
191
- * Uses binary search for efficient size calculation
192
- */
193
- updateLabelSize() {
194
- let maxSize = min(this.width * 0.9, this.height * 0.8);
195
- let minSize = 1;
196
- let low = minSize;
197
- let high = maxSize;
198
- let bestSize = minSize;
199
-
200
- const maxLabelWidth = this.width * 0.9;
201
- const maxLabelHeight = this.height * 0.8;
202
-
203
- while (low <= high) {
204
- let mid = Math.floor((low + high) / 2);
205
- textSize(mid);
206
- let labelWidth = textWidth(this.text);
207
- let labelHeight = textAscent() + textDescent();
208
-
209
- if (labelWidth <= maxLabelWidth && labelHeight <= maxLabelHeight) {
210
- bestSize = mid;
211
- low = mid + 1;
212
- } else {
213
- high = mid - 1;
214
- }
215
- }
216
-
217
- this.labelSize = bestSize;
218
- }
219
-
220
- /**
221
- * Handles width changes and updates text size accordingly
222
- */
223
- updateWidth(){
224
- this.updateLabelSize();
225
- }
226
-
227
- /**
228
- * Handles height changes and updates text size accordingly
229
- */
230
- updateHeight(){
231
- this.updateLabelSize();
232
- }
233
177
  }
234
178