ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
ImageView.c
Go to the documentation of this file.
1/*
2 * ObjectivelyMVC: Object oriented MVC framework for SDL3 and C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
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 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25
26#include "ImageAtlas.h"
27#include "ImageView.h"
28#include "Theme.h"
29
30const EnumName SDLGPUBlendFactorNames[] = MakeEnumNames(
31 MakeEnumName(SDL_GPU_BLENDFACTOR_ZERO),
32 MakeEnumName(SDL_GPU_BLENDFACTOR_ONE),
33 MakeEnumName(SDL_GPU_BLENDFACTOR_SRC_COLOR),
34 MakeEnumName(SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_COLOR),
35 MakeEnumName(SDL_GPU_BLENDFACTOR_DST_COLOR),
36 MakeEnumName(SDL_GPU_BLENDFACTOR_ONE_MINUS_DST_COLOR),
37 MakeEnumName(SDL_GPU_BLENDFACTOR_SRC_ALPHA),
38 MakeEnumName(SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA),
39 MakeEnumName(SDL_GPU_BLENDFACTOR_DST_ALPHA),
40 MakeEnumName(SDL_GPU_BLENDFACTOR_ONE_MINUS_DST_ALPHA),
41 MakeEnumName(SDL_GPU_BLENDFACTOR_CONSTANT_COLOR),
42 MakeEnumName(SDL_GPU_BLENDFACTOR_ONE_MINUS_CONSTANT_COLOR),
43 MakeEnumName(SDL_GPU_BLENDFACTOR_SRC_ALPHA_SATURATE)
44);
45
46#define _Class _ImageView
47
48#pragma mark - Object
49
53static void dealloc(Object *self) {
54
55 ImageView *this = (ImageView *) self;
56
57 release(this->image);
58
59 free(this->imageName);
60
61 this->texture = release(this->texture);
62
63 super(Object, self, dealloc);
64}
65
66#pragma mark - View
67
71static void applyStyle(View *self, const Style *style) {
72
73 super(View, self, applyStyle, style);
74
75 ImageView *this = (ImageView *) self;
76
77 if (this->image) {
78
79 const bool width = $(style->attributes, objectForKeyPath, "width") != NULL;
80 const bool height = $(style->attributes, objectForKeyPath, "height") != NULL;
81
82 SDL_Size size = $(self, size);
83 const SDL_Size imageSize = $(this->image, size);
84
85 // the styled axis is clamped first, so that min and max sizes do not distort the aspect ratio
86 if (width && !height) {
87 size.w = ClampSize(size.w, self->minSize.w, self->maxSize.w);
88 size.h = size.w * imageSize.h / (float) imageSize.w;
89 $(self, resize, &size);
90 } else if (height && !width) {
91 size.h = ClampSize(size.h, self->minSize.h, self->maxSize.h);
92 size.w = size.h * imageSize.w / (float) imageSize.h;
93 $(self, resize, &size);
94 } else if (size.w == 0 && size.h == 0) {
95 $(self, resize, &imageSize);
96 }
97 }
98}
99
103static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
104
105 super(View, self, awakeWithDictionary, dictionary);
106
107 ImageView *this = (ImageView *) self;
108
109 const Inlet inlets[] = MakeInlets(
110 MakeInlet("blend.src", InletTypeEnum, &this->blend.src, (ident) SDLGPUBlendFactorNames),
111 MakeInlet("blend.dst", InletTypeEnum, &this->blend.dst, (ident) SDLGPUBlendFactorNames),
112 MakeInlet("color", InletTypeColor, &this->color, NULL),
113 MakeInlet("image", InletTypeCharacters, &this->imageName, NULL)
114 );
115
116 $(self, bind, inlets, dictionary);
117}
118
122static void didMoveToWindow(View *self, SDL_Window *window) {
123
124 super(View, self, didMoveToWindow, window);
125
126 ImageView *this = (ImageView *) self;
127
128 // leaving a window resolves nothing: there is no density to resolve against, and View::dealloc
129 // moves to a NULL window, where loading an image would be worse than pointless
130 if (window == NULL) {
131 return;
132 }
133
134 if (this->imageName == NULL) {
135 return;
136 }
137
138 char *name = this->imageName;
139 this->imageName = NULL;
140
141 Theme *theme = $$(Theme, theme, window);
142 if (theme) {
143 Image *image = $(theme, image, name, SDL_GetWindowPixelDensity(window));
144 $(this, setImage, image);
145 } else {
146 Image *image = $$(Image, imageWithResourceName, name, SDL_GetWindowPixelDensity(window));
147 $(this, setImage, image);
148 release(image);
149 }
150
151 this->imageName = name;
152}
153
157static View *init(View *self) {
158 return (View *) $((ImageView *) self, initWithImage, NULL);
159}
160
164static void render(View *self, Renderer *renderer) {
165
166 super(View, self, render, renderer);
167
168 ImageView *this = (ImageView *) self;
169
170 Texture *texture = NULL;
171 SDL_Rect region, *src = NULL;
172
173 if (this->image) {
174 if ($((Object *) this->image, isKindOfClass, _AtlasImage())) {
175
176 // The atlas owns the Texture, and repacks on compile, so neither is cached here
177 AtlasImage *atlasImage = (AtlasImage *) this->image;
178 if (atlasImage->atlas) {
179 texture = $(atlasImage->atlas, texture, renderer->device);
180 region = atlasImage->rect;
181 src = &region;
182 }
183
184 } else {
185 if (this->texture == NULL) {
186 this->texture = $(renderer->device, createTextureFromSurface,
187 this->image->surface, SDL_GPU_TEXTUREUSAGE_SAMPLER, false);
188 assert(this->texture);
189 }
190 texture = this->texture;
191 }
192 } else {
193 texture = this->texture;
194 }
195
196 if (texture) {
197
198 // TODO: Actually use self->blend
199
200 const SDL_Rect frame = $(self, renderFrame);
201
202 SDL_FRect frect;
203 SDL_RectToFRect(&frame, &frect);
204
205 if (self->borderRadius > 0) {
206 $(renderer, drawRoundedTextureRegion, texture, src, &frect, self->borderRadius, &this->color);
207 } else {
208 $(renderer, drawTextureRegion, texture, src, &frect, &this->color);
209 }
210 }
211}
212
216static void renderDeviceWillReset(View *self) {
217
218 ImageView *this = (ImageView *) self;
219
220 this->texture = release(this->texture);
221
222 if (this->image && $((Object *) this->image, isKindOfClass, _AtlasImage())) {
223 ImageAtlas *atlas = ((AtlasImage *) this->image)->atlas;
224 if (atlas) {
225 $(atlas, renderDeviceWillReset);
226 }
227 }
228
229 super(View, self, renderDeviceWillReset);
230}
231
232#pragma mark - ImageView
233
238static ImageView *initWithFrame(ImageView *self, const SDL_Rect *frame) {
239
240 self = (ImageView *) super(View, self, initWithFrame, frame);
241 if (self) {
242 self->blend.src = SDL_GPU_BLENDFACTOR_SRC_ALPHA;
243 self->blend.dst = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA;
244
245 self->color = Colors.White;
246 }
247
248 return self;
249}
250
256
257 self = (ImageView *) $(self, initWithFrame, NULL);
258 if (self) {
259 $(self, setImage, image);
260 }
261
262 return self;
263}
264
269static void setImage(ImageView *self, Image *image) {
270
271 free(self->imageName);
272 self->imageName = NULL;
273
274 release(self->image);
275
276 if (image) {
277 self->image = retain(image);
278 } else {
279 self->image = NULL;
280 }
281
282 self->texture = release(self->texture);
283
284 $((View *) self, invalidateStyle);
285}
286
291static void setImageWithResource(ImageView *self, const Resource *resource) {
292
293 Image *image = $$(Image, imageWithResource, resource, 1.f);
294
295 $(self, setImage, image);
296
297 release(image);
298}
299
304static void setImageWithResourceName(ImageView *self, const char *name) {
305
306 $(self, setImage, NULL);
307
308 // an empty name is no image, not a request to ask the resource system for ""
309 if (name && *name) {
310 self->imageName = strdup(name);
311
312 View *this = (View *) self;
313 if (this->window) {
314 $(this, didMoveToWindow, this->window);
315 }
316 }
317}
318
323static void setImageWithSurface(ImageView *self, SDL_Surface *surface) {
324
325 $(self, setImage, NULL);
326
327 if (surface) {
328 Image *image = $(alloc(Image), initWithSurface, surface);
329 assert(image);
330
331 $(self, setImage, image);
332
333 release(image);
334 }
335}
336
337#pragma mark - Class lifecycle
338
342static void initialize(Class *clazz) {
343
344 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
345
346 ((ViewInterface *) clazz->interface)->applyStyle = applyStyle;
347 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
348 ((ViewInterface *) clazz->interface)->didMoveToWindow = didMoveToWindow;
349 ((ViewInterface *) clazz->interface)->init = init;
350 ((ViewInterface *) clazz->interface)->render = render;
351 ((ViewInterface *) clazz->interface)->renderDeviceWillReset = renderDeviceWillReset;
352
353 ((ImageViewInterface *) clazz->interface)->initWithFrame = initWithFrame;
354 ((ImageViewInterface *) clazz->interface)->initWithImage = initWithImage;
355 ((ImageViewInterface *) clazz->interface)->setImage = setImage;
356 ((ImageViewInterface *) clazz->interface)->setImageWithResource = setImageWithResource;
357 ((ImageViewInterface *) clazz->interface)->setImageWithResourceName = setImageWithResourceName;
358 ((ImageViewInterface *) clazz->interface)->setImageWithSurface = setImageWithSurface;
359}
360
365Class *_ImageView(void) {
366 static Class *clazz;
367 static Once once;
368
369 do_once(&once, {
370 clazz = _initialize(&(const ClassDef) {
371 .name = "ImageView",
372 .superclass = _View(),
373 .instanceSize = sizeof(ImageView),
374 .interfaceSize = sizeof(ImageViewInterface),
375 .initialize = initialize,
376 });
377 });
378
379 return clazz;
380}
381
382#undef _Class
static AtlasImage * initWithImage(AtlasImage *self, ImageAtlas *atlas, Image *image)
Definition AtlasImage.c:32
Class * _AtlasImage(void)
Definition AtlasImage.c:84
static void initialize(Class *clazz)
Definition AtlasImage.c:74
static View * init(View *self)
Definition Box.c:67
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition Box.c:50
static void dealloc(Object *self)
Definition Box.c:35
static Box * initWithFrame(Box *self, const SDL_Rect *frame)
Definition Box.c:92
static void applyStyle(View *self, const Style *style)
static void render(View *self, Renderer *renderer)
Definition Control.c:181
static String * name(const Font *self)
Definition Font.c:303
static Image * initWithSurface(Image *self, SDL_Surface *surface)
Definition Image.c:391
static Image * imageWithResource(const Resource *resource, float pixelDensity)
Definition Image.c:66
static Image * imageWithResourceName(const char *name, float pixelDensity)
Definition Image.c:74
static SDL_Size size(const Image *self)
Definition Image.c:416
static void renderDeviceWillReset(ImageAtlas *self)
Definition ImageAtlas.c:282
static Texture * texture(ImageAtlas *self, RenderDevice *device)
Definition ImageAtlas.c:290
Packs many Images into one sheet, backed by one Texture.
static void setImageWithResource(ImageView *self, const Resource *resource)
Definition ImageView.c:291
static void didMoveToWindow(View *self, SDL_Window *window)
Definition ImageView.c:122
static void setImageWithSurface(ImageView *self, SDL_Surface *surface)
Definition ImageView.c:323
static View * init(View *self)
Definition ImageView.c:157
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition ImageView.c:103
static ImageView * initWithFrame(ImageView *self, const SDL_Rect *frame)
Definition ImageView.c:238
static void setImageWithResourceName(ImageView *self, const char *name)
Definition ImageView.c:304
static void applyStyle(View *self, const Style *style)
Definition ImageView.c:71
static void setImage(ImageView *self, Image *image)
Definition ImageView.c:269
static void render(View *self, Renderer *renderer)
Definition ImageView.c:164
static ImageView * initWithImage(ImageView *self, Image *image)
Definition ImageView.c:255
static void dealloc(Object *self)
Definition ImageView.c:53
const EnumName SDLGPUBlendFactorNames[]
Definition ImageView.c:30
static void initialize(Class *clazz)
Definition ImageView.c:342
static void renderDeviceWillReset(View *self)
Definition ImageView.c:216
Class * _ImageView(void)
Definition ImageView.c:365
ImageViews render an Image in the context of a View hierarchy.
static void drawTextureRegion(const Renderer *self, Texture *texture, const SDL_Rect *src, const SDL_FRect *dest, const SDL_Color *color)
Definition Renderer.c:497
static void drawRoundedTextureRegion(const Renderer *self, Texture *texture, const SDL_Rect *src, const SDL_FRect *dest, int radius, const SDL_Color *color)
Definition Renderer.c:473
static Theme * theme(SDL_Window *window)
Definition Theme.c:295
static Image * image(Theme *self, const char *name, float pixelDensity)
Definition Theme.c:82
The Theme type.
@ InletTypeEnum
Definition View+JSON.h:75
@ InletTypeCharacters
Definition View+JSON.h:52
@ InletTypeColor
Definition View+JSON.h:62
#define MakeInlets(...)
Creates a null-termianted array of Inlets.
Definition View+JSON.h:215
#define MakeInlet(name, type, dest, data)
Creates an Inlet with the specified parameters.
Definition View+JSON.h:210
static void resize(View *self, const SDL_Size *size)
Definition View.c:1708
Class * _View(void)
Definition View.c:2338
static SDL_Rect renderFrame(const View *self)
Definition View.c:1629
#define ClampSize(size, min, max)
Clamps size to min and max, where a max of zero is unbounded.
Definition View.h:103
An Image packed into an ImageAtlas.
Definition AtlasImage.h:41
SDL_Rect rect
This AtlasImage's rectangle within the atlas sheet, in texels.
Definition AtlasImage.h:66
ImageAtlas * atlas
The ImageAtlas this AtlasImage belongs to, or NULL once that atlas has been released,...
Definition AtlasImage.h:59
W3C Color constants.
Definition Colors.h:37
SDL_Color White
Definition Colors.h:185
Packs many Images into one sheet, backed by one Texture.
Definition ImageAtlas.h:49
Image loading.
Definition Image.h:41
ImageViews render an Image in the context of a View hierarchy.
Definition ImageView.h:45
Image * image
The image.
Definition ImageView.h:73
ImageView * initWithFrame(ImageView *self, const SDL_Rect *frame)
Initializes this ImageView with the specified frame.
Definition ImageView.c:238
struct ImageView::@057055105372320051011063062227120011174355046077 blend
The blend factors (currently unused; reserved for future per-pipeline support).
void setImage(ImageView *self, Image *image)
Sets the Image for this ImageView.
Definition ImageView.c:269
SDL_GPUBlendFactor src
Definition ImageView.h:62
Texture * texture
The GPU texture.
Definition ImageView.h:89
SDL_GPUBlendFactor dst
Definition ImageView.h:62
SDL_Color color
The drawing color.
Definition ImageView.h:68
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:149
Renderer extends Object with ObjectivelyMVC's UI rendering layer.
Definition Renderer.h:75
RenderDevice * device
The backing RenderDevice.
Definition Renderer.h:97
The Style type.
Definition Style.h:43
Dictionary * attributes
Definition Style.h:59
The Theme type.
Definition Theme.h:54
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:225
void invalidateStyle(View *self)
Invalidates the computed Style for this View and its descendants.
Definition View.c:1092
SDL_Window * window
The window.
Definition View.h:480
void renderDeviceWillReset(View *self)
Informs this View that the render device will reset.
Definition ImageView.c:216
SDL_Size minSize
The minimum size this View may be resized to during layout.
Definition View.h:345
void didMoveToWindow(View *self, SDL_Window *window)
Informs this View that it has been added to the View hierachy of the given window.
Definition ImageView.c:122
int borderRadius
The corner radius applied to the background, border and bevel.
Definition View.h:281
SDL_Size maxSize
The maximum size this View may be resized to during layout.
Definition View.h:339
SDL_Rect frame
The frame, relative to the superview.
Definition View.h:301