Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Todo:
  3.         - zeskalowanie aktywnych po zmianie rozdzielczosci
  4.         - refaktoryzacja kodu
  5. */
  6.  
  7. const NOTIFICATION_DISPLAY_TIME = 2500//ms
  8. const NOTIFICATION_ALPHA_EFFECT = 20//percent (długość chowania/pokazania, efekty alfy)
  9. const NOTIFICATION_DISPLAY_SIZE = 30//procent
  10.  
  11. addEvent("onNotificationDestroyItem")
  12.  
  13. local deltaTime = getTickCount()
  14. local lengthMoving = 0//px
  15. local endPosition = 0//px
  16.  
  17. local sound = Sound("LEVER_LOCKED.WAV");
  18. sound.looping = false;
  19. sound.balance = 1.0;
  20.  
  21.  
  22. class GuiEditor.NotificationItem extends GUI.Draw//zrobić destruktor w klase GUI.Draw
  23. {
  24. #private:
  25.     m_currMilisecond = 0
  26.     m_isOutStart = true
  27.  
  28.     constructor(text, colorCoded = false)
  29.     {
  30.         base.constructor(0, 0, "")
  31.         setText(text, colorCoded)
  32.     }
  33.  
  34.     function setText(text, colorCoded = false)//musi być update pozycji (zmieia się szerokość, kontruktor nie uwzględnia bbcode)
  35.     {
  36.         base.setText(text, colorCoded)
  37.  
  38.         local res = getResolution()
  39.         base.setPositionPx(res.x / 2 - base.getSizePx().width / 2, res.y / 2 - textHeightPx(text) / 2)
  40.     }
  41.  
  42.     function getPercentFromNumber(value, percent)
  43.     {
  44.         return value.tofloat()/100.0 * percent
  45.     }
  46.  
  47.     function getPercentFromRange(value, maxValue)
  48.     {
  49.         return value.tofloat() / maxValue.tofloat() * 100
  50.     }
  51.  
  52.     function isOutStart()
  53.     {
  54.         return m_isOutStart
  55.     }
  56.  
  57.     function effectRender()
  58.     {
  59.         local progress = getPercentFromRange(m_currMilisecond, NOTIFICATION_DISPLAY_TIME)
  60.         local range = getPercentFromNumber(NOTIFICATION_DISPLAY_TIME, NOTIFICATION_ALPHA_EFFECT)
  61.  
  62.         if (progress < NOTIFICATION_ALPHA_EFFECT)
  63.         {
  64.             local speed = 255.0 / range.tofloat()
  65.             base.setAlpha(speed * m_currMilisecond)
  66.         }
  67.  
  68.         if (progress > 100 - NOTIFICATION_ALPHA_EFFECT)
  69.         {
  70.             local multiplier = m_currMilisecond - (NOTIFICATION_DISPLAY_TIME - range)
  71.  
  72.             local speed = 255.0 / range.tofloat()
  73.             base.setAlpha((255 - speed * multiplier))
  74.         }
  75.  
  76.         local pos = getPositionPx()
  77.  
  78.         base.setPositionPx(pos.x, (endPosition + lengthMoving - base.getLineSizePx()) - (lengthMoving / NOTIFICATION_DISPLAY_TIME * m_currMilisecond))
  79.  
  80.         if ((pos.y < endPosition + lengthMoving - 2 * getLineSizePx()) && m_isOutStart)
  81.             m_isOutStart = false
  82.  
  83.         if(!base.getVisible())
  84.             base.setVisible(true)
  85.     }
  86.  
  87.     function render()
  88.     {
  89.         if (m_currMilisecond == NOTIFICATION_DISPLAY_TIME)
  90.             callEvent("onNotificationDestroyItem", this)
  91.         else
  92.         {
  93.             ++m_currMilisecond
  94.             effectRender()
  95.         }
  96.     }
  97. }
  98.  
  99. class GuiEditor.Notification
  100. {
  101.     static m_queue = []
  102.     static m_visibles = []
  103.  
  104.     static function init()//poprawić
  105.     {
  106.         local resolution = getResolution()
  107.  
  108.         lengthMoving = resolution.y.tofloat() / 100.0 * NOTIFICATION_DISPLAY_SIZE
  109.         endPosition = resolution.y / 2 - lengthMoving / 2
  110.     }
  111.  
  112.     static function add(message, colorCoded = false)
  113.     {
  114.         if (m_visibles.len() == 0)
  115.         {
  116.             m_visibles.push(GuiEditor.NotificationItem(message, colorCoded))
  117.             sound.play()// :D
  118.             return
  119.         }
  120.  
  121.         if (m_visibles.len() < lengthMoving / textHeightPx(""))
  122.             if (!m_visibles[m_visibles.len() - 1].isOutStart())
  123.                 m_visibles.push(GuiEditor.NotificationItem(message, colorCoded))
  124.         else
  125.             m_queue.push({message = message, colorCoded = colorCoded});
  126.            
  127.         sound.play()
  128.     }
  129.  
  130.     static function onNotificationDestroyItem(item)
  131.     {
  132.         local visibles = GuiEditor.Notification.m_visibles
  133.  
  134.         for(local i = 0; i < visibles.len(); ++i)
  135.         {
  136.             if (item == visibles[i])
  137.             {
  138.                 item.destroy()
  139.                 visibles.remove(i)
  140.                 return
  141.             }
  142.         }
  143.     }
  144.  
  145.     static function onRender()
  146.     {
  147.         if (deltaTime <= getTickCount())
  148.         {
  149.             deltaTime = getTickCount() - deltaTime + 1;
  150.  
  151.             local visibles = GuiEditor.Notification.m_visibles
  152.             local items = GuiEditor.Notification.m_queue
  153.  
  154.             foreach(object in visibles)
  155.                 object.render()
  156.            
  157.             if (visibles.len() > 0 && items.len() > 0)
  158.             {
  159.                 if(!visibles[visibles.len() - 1].isOutStart())
  160.                 {
  161.                     visibles.push(GuiEditor.NotificationItem(items[0].message, items[0].colorCoded))
  162.                     items.remove(0)//usunięcie z kolejki
  163.                 }
  164.             }
  165.         }
  166.     }
  167. }
  168.  
  169. addEventHandler("onNotificationDestroyItem", GuiEditor.Notification.onNotificationDestroyItem)
  170. addEventHandler("onRender", GuiEditor.Notification.onRender)
  171. addEventHandler("onInit", GuiEditor.Notification.init)
  172.  
  173. //test
  174.  
  175. addEventHandler("onKey", function(key)
  176. {
  177.     if (key == KEY_H)
  178.         GuiEditor.Notification.add("[#FF7F00](GuiEditor) [#F60005]Włączono", true)
  179.  
  180.     if (key == KEY_G)
  181.         GuiEditor.Notification.add("[#FF7F00](GuiEditor) [#32CD32]Wyłączono", true)
  182. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement