Welcome to mirror list, hosted at ThFree Co, Russian Federation.

im_image.cpp « IMUI « GUI « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cbf4337c1fde44926931196773ad7c0ced9f9972 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//-----------------------------------------------------------------------------
//           Name: im_image.cpp
//      Developer: Wolfire Games LLC
//    Description: Image element class for creating adhoc GUIs as part of
//                 the UI tools
//        License: Read below
//-----------------------------------------------------------------------------
//
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#include "im_image.h"

#include <GUI/IMUI/imgui.h>
#include <Math/vec2math.h>

/*******************************************************************************************/
/**
 * @brief  Constructor
 *
 */
IMImage::IMImage() : rotation(0.0),
                     skip_aspect_fitting(false),
                     center(false)

{
    IMrefCountTracker.addRefCountObject(getElementTypeName());
    setColor(vec4(1.0, 1.0, 1.0, 1.0));
}

/*******************************************************************************************/
/**
 * @brief  Copy constructor
 *
 */
IMImage::IMImage(IMImage const& other) {
    IMrefCountTracker.addRefCountObject(getElementTypeName());

    imageFileName = other.imageFileName;
    originalImageSize = other.originalImageSize;
    rotation = other.rotation;
    skip_aspect_fitting = other.skip_aspect_fitting;
    center = other.center;
    textureOffset = other.textureOffset;
    textureSize = other.textureSize;
}

/*******************************************************************************************/
/**
 * @brief  Constructor
 *
 * @param imageName Filename for the image
 *
 */
IMImage::IMImage(std::string const& imageName) : IMElement(imageName),
                                                 rotation(0.0),
                                                 skip_aspect_fitting(false),
                                                 center(false) {
    IMrefCountTracker.addRefCountObject(getElementTypeName());

    setImageFile(imageName);
    setColor(vec4(1.0, 1.0, 1.0, 1.0));
}

/*******************************************************************************************/
/**
 * @brief  Constructor
 *
 * @param imageName Path for the image
 *
 */
IMImage::IMImage(std::string const& imageName, bool abs_path) : IMElement(imageName),
                                                                rotation(0.0),
                                                                skip_aspect_fitting(false),
                                                                center(false) {
    IMrefCountTracker.addRefCountObject(getElementTypeName());

    if (abs_path) {
        setImageFileAbs(imageName);
    } else {
        setImageFile(imageName);
    }
    setColor(vec4(1.0, 1.0, 1.0, 1.0));
}

/*******************************************************************************************/
/**
 * @brief  Gets the name of the type of this element — for autonaming and debugging
 *
 * @returns name of the element type as a string
 *
 */
std::string IMImage::getElementTypeName() {
    return "Image";
}

void IMImage::setSkipAspectFitting(bool val) {
    skip_aspect_fitting = val;
}

void IMImage::setCenter(bool val) {
    center = val;
}

/*******************************************************************************************/
/**
 * @brief  Sets the source for the image
 *
 * @param _fileName
 *
 */
void IMImage::setImageFile(std::string const& _fileName) {
    if (_fileName == "") {
        return;
    }

    imageFileName = "Data/" + _fileName;
    if (!imuiImage.loadImage(imageFileName)) {
        IMDisplayError("Error", std::string("Unable to locate image " + imageFileName));
        return;
    }

    // Get the size
    originalImageSize = vec2(int(imuiImage.getTextureWidth()),
                             int(imuiImage.getTextureHeight()));

    setSize(originalImageSize);
}

void IMImage::setImageFileAbs(std::string const& _fileName) {
    if (_fileName == "") {
        return;
    }

    imageFileName = _fileName;
    if (!imuiImage.loadImage(imageFileName)) {
        IMDisplayError("Error", std::string("Unable to locate image " + imageFileName));
        return;
    }

    // Get the size
    originalImageSize = vec2(int(imuiImage.getTextureWidth()),
                             int(imuiImage.getTextureHeight()));

    setSize(originalImageSize);
}

/*******************************************************************************************/
/**
 * @brief  Rescales the image to a specified width
 *
 * @param newSize new x size
 *
 */
void IMImage::scaleToSizeX(float newSize) {
    float newYSize = (originalImageSize.y() / originalImageSize.x()) * newSize;
    setSize(vec2(newSize, newYSize));
}

/*******************************************************************************************/
/**
 * @brief  Rescales the image to a specified height
 *
 * @param newSize new y size
 *
 */
void IMImage::scaleToSizeY(float newSize) {
    float newXSize = (originalImageSize.x() / originalImageSize.y()) * newSize;
    setSize(vec2(newXSize, newSize));
}

/*******************************************************************************************/
/**
 * @brief  Rather counter-intuitively, this draws this object on the screen
 *
 * @param drawOffset Absolute offset from the upper lefthand corner (GUI space)
 * @param clipPos pixel location of upper lefthand corner of clipping region
 * @param clipSize size of clipping region
 *
 */
void IMImage::render(vec2 drawOffset, vec2 currentClipPos, vec2 currentClipSize) {
    Graphics* graphics = Graphics::Instance();

    // Make sure we have an an image and we're supposed draw it
    if (imageFileName != "" && show) {
        vec2 GUIRenderPos = vec2(0, 0);

        imuiImage.skip_aspect_fitting = skip_aspect_fitting;

        if (center) {
            float diff_x = (float)graphics->window_dims[0] - getSizeX();
            float diff_y = (float)graphics->window_dims[1] - getSizeY();
            GUIRenderPos = vec2(diff_x / 2.0f, diff_y / 2.0f);
        } else {
            GUIRenderPos = drawOffset + drawDisplacement;
        }

        if (skip_aspect_fitting) {
            imuiImage.setRenderSize(vec2(getSizeX(), getSizeY()));

            imuiImage.setPosition(vec3(GUIRenderPos.x(), GUIRenderPos.y(), (float)getZOrdering()));
        } else {
            vec2 screenRenderPos = screenMetrics.GUIToScreen(GUIRenderPos);

            imuiImage.setRenderSize(vec2((float(getSizeX()) * screenMetrics.GUItoScreenXScale),
                                         (float(getSizeY()) * screenMetrics.GUItoScreenYScale)));

            imuiImage.setPosition(vec3(screenRenderPos.x(), screenRenderPos.y(), (float)getZOrdering()));
        }

        if (isColorEffected) {
            imuiImage.setColor(effectColor);
        } else {
            imuiImage.setColor(color);
        }

        imuiImage.setRotation(rotation);

        if (textureSize.x() != 0 && textureSize.y() != 0) {
            imuiImage.setRenderOffset(vec2(textureOffset.x(), textureOffset.y()),
                                      vec2(textureSize.x(), textureSize.y()));
        }

        // Add clipping (if we need it)
        if (shouldClip && currentClipSize.x() != UNDEFINEDSIZE && currentClipSize.y() != UNDEFINEDSIZE) {
            vec2 adjustedClipPos = screenMetrics.GUIToScreen(currentClipPos);
            imuiImage.setClipping(vec2(adjustedClipPos.x(),
                                       adjustedClipPos.y()),
                                  vec2(currentClipSize.x() * screenMetrics.GUItoScreenXScale,
                                       currentClipSize.y() * screenMetrics.GUItoScreenYScale));
        }

        if (owner != NULL) {
            owner->IMGUI_IMUIContext->queueImage(imuiImage);
        }
    }

    // Call the superclass to make sure any element specific rendering is done
    IMElement::render(drawOffset, currentClipPos, currentClipSize);
}

/*******************************************************************************************/
/**
 * @brief  Updates the element
 *
 * @param delta Number of millisecond elapsed since last update
 * @param drawOffset Absolute offset from the upper lefthand corner (GUI space)
 * @param guistate The state of the GUI at this update
 *
 */
void IMImage::update(uint64_t delta, vec2 drawOffset, GUIState& guistate) {
    IMElement::update(delta, drawOffset, guistate);
}

/*******************************************************************************************/
/**
 * @brief Sets the rotation for this image
 *
 * @param Rotation (in degrees)
 *
 */
void IMImage::setRotation(float _rotation) {
    rotation = _rotation;
    imuiImage.setRotation(rotation);
}

/*******************************************************************************************/
/**
 * @brief Gets the rotation for this image
 *
 * @returns current rotation (in degrees)
 *
 */
float IMImage::getRotation() {
    return rotation;
}

/*******************************************************************************************/
/**
 * @brief  Render only part of this image
 *
 * (all coordinates are based on the original size of the image)
 *
 * @param offset position of the upper lefthand coordinate to source the rendering from
 * @param size size of the rectangle to use for the rendering
 *
 */
void IMImage::setImageOffset(vec2 offset, vec2 size) {
    textureOffset = offset;
    textureSize = size;
}

/*******************************************************************************************/
/**
 * @brief  Remove all referenced object without releaseing references
 *
 */
void IMImage::clense() {
    IMElement::clense();
}

/*******************************************************************************************/
/**
 * @brief  Destructor
 *
 */
IMImage::~IMImage() {
    IMrefCountTracker.removeRefCountObject(getElementTypeName());
}