LCOV - code coverage report
Current view: top level - source/gui/SettingTypes - CGUISize.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 71 74 95.9 %
Date: 2026-12-03 01:19:39 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "CGUISize.h"
      21             : 
      22             : #include "ps/CLogger.h"
      23             : #include "ps/CStr.h"
      24             : 
      25          33 : CGUISize::CGUISize()
      26          33 :     : pixel(), percent()
      27             : {
      28          33 : }
      29             : 
      30           6 : CGUISize::CGUISize(const CRect& pixel, const CRect& percent)
      31           6 :     : pixel(pixel), percent(percent)
      32             : {
      33           6 : }
      34             : 
      35           0 : CGUISize CGUISize::Full()
      36             : {
      37           0 :     return CGUISize(CRect(0, 0, 0, 0), CRect(0, 0, 100, 100));
      38             : }
      39             : 
      40           2 : CRect CGUISize::GetSize(const CRect& parent) const
      41             : {
      42             :     // If it's a 0 0 100% 100% we need no calculations
      43           2 :     if (percent == CRect(0.f, 0.f, 100.f, 100.f) && pixel == CRect())
      44           0 :         return parent;
      45             : 
      46           2 :     CRect client;
      47             : 
      48             :     // This should probably be cached and not calculated all the time for every object.
      49           2 :     client.left =   parent.left + (parent.right-parent.left)*percent.left/100.f + pixel.left;
      50           2 :     client.top =    parent.top + (parent.bottom-parent.top)*percent.top/100.f + pixel.top;
      51           2 :     client.right =  parent.left + (parent.right-parent.left)*percent.right/100.f + pixel.right;
      52           2 :     client.bottom = parent.top + (parent.bottom-parent.top)*percent.bottom/100.f + pixel.bottom;
      53             : 
      54           2 :     return client;
      55             : }
      56             : 
      57          12 : bool CGUISize::FromString(const CStr& Value)
      58             : {
      59             :     /*
      60             :      * GUISizes contain a left, top, right, and bottom
      61             :      *  for example: "50%-150 10%+9 50%+150 10%+25" means
      62             :      *  the left edge is at 50% minus 150 pixels, the top
      63             :      *  edge is at 10% plus 9 pixels, the right edge is at
      64             :      *  50% plus 150 pixels, and the bottom edge is at 10%
      65             :      *  plus 25 pixels.
      66             :      * All four coordinates are required and can be
      67             :      *  defined only in pixels, only in percents, or some
      68             :      *  combination of both.
      69             :      */
      70             : 
      71             :     // Check the input is only numeric
      72          12 :     const char* input = Value.c_str();
      73          12 :     CStr buffer;
      74          12 :     unsigned int coord = 0;
      75          12 :     float pixels[4] = {0, 0, 0, 0};
      76          12 :     float percents[4] = {0, 0, 0, 0};
      77         197 :     for (unsigned int i = 0; i < Value.length(); ++i)
      78             :     {
      79         189 :         switch (input[i])
      80             :         {
      81         113 :         case '.':
      82         113 :         case '0':
      83         113 :         case '1':
      84         113 :         case '2':
      85         113 :         case '3':
      86         113 :         case '4':
      87         113 :         case '5':
      88         113 :         case '6':
      89         113 :         case '7':
      90         113 :         case '8':
      91         113 :         case '9':
      92         113 :             buffer.push_back(input[i]);
      93             :             break;
      94           5 :         case '+':
      95           5 :             pixels[coord] += buffer.ToFloat();
      96           5 :             buffer = "+";
      97           5 :             break;
      98          20 :         case '-':
      99          20 :             pixels[coord] += buffer.ToFloat();
     100          20 :             buffer = "-";
     101          20 :             break;
     102          20 :         case '%':
     103          20 :             percents[coord] += buffer.ToFloat();
     104          20 :             buffer = "";
     105          20 :             break;
     106          28 :         case ' ':
     107          28 :             pixels[coord] += buffer.ToFloat();
     108          28 :             buffer = "";
     109          28 :             ++coord;
     110          28 :             break;
     111           3 :         default:
     112           6 :             LOGERROR("CGUISize definition may only include numbers. Your input: '{}'", Value);
     113           3 :             return false;
     114             :         }
     115         186 :         if (coord > 3)
     116             :         {
     117           2 :             LOGERROR("Too many CGUISize parameters (4 max). Your input: '{}'", Value);
     118           1 :             return false;
     119             :         }
     120             :     }
     121             : 
     122           8 :     if (coord < 3)
     123             :     {
     124           2 :         LOGERROR("Too few CGUISize parameters (4 min). Your input: '{}'", Value);
     125           1 :         return false;
     126             :     }
     127             : 
     128             :     // Now that we're at the end of the string, flush the remaining buffer.
     129           7 :     pixels[coord] += buffer.ToFloat();
     130             : 
     131             :     // Now store the coords in the right place
     132           7 :     pixel.left =        pixels[0];
     133           7 :     pixel.top =         pixels[1];
     134           7 :     pixel.right =       pixels[2];
     135           7 :     pixel.bottom =      pixels[3];
     136           7 :     percent.left =      percents[0];
     137           7 :     percent.top =       percents[1];
     138           7 :     percent.right =     percents[2];
     139           7 :     percent.bottom =    percents[3];
     140           7 :     return true;
     141          12 : }

Generated by: LCOV version 1.16