Advertisement
Guest User

Untitled

a guest
Feb 25th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.55 KB | None | 0 0
  1. -- Terrain Editor UI
  2.  
  3. TerrainEditUI=ScriptObject()
  4.  
  5. uiStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  6. ui.root.defaultStyle = uiStyle;
  7. iconStyle = cache:GetResource("XMLFile", "UI/EditorIcons.xml");
  8.  
  9. function CreateCursor()
  10.     local cursor = Cursor:new("Cursor")
  11.     --cursor.defaultStyle=uiStyle
  12.     --cursor.style=AUTO_STYLE
  13.     cursor:SetStyleAuto(uiStyle)
  14.     cursor:SetPosition(graphics.width / 2, graphics.height / 2)
  15.     ui.cursor = cursor
  16.     cursor.visible=true
  17.  
  18. end
  19.  
  20. function TerrainEditUI:Start()
  21.     self.heightbrush=ui:LoadLayout(cache:GetResource("XMLFile", "UI/TerrainEditHeightBrush.xml"))
  22.     self.blendbrush=ui:LoadLayout(cache:GetResource("XMLFile", "UI/TerrainEditBlendBrush.xml"))
  23.     self.maskbrush=ui:LoadLayout(cache:GetResource("XMLFile", "UI/TerrainEditMaskBrush.xml"))
  24.     self.smoothbrush=ui:LoadLayout(cache:GetResource("XMLFile", "UI/TerrainEditSmoothBrush.xml"))
  25.     self.newterrain=ui:LoadLayout(cache:GetResource("XMLFile", "UI/TerrainEditNewTerrain.xml"))
  26.     self.toolbar=ui:LoadLayout(cache:GetResource("XMLFile", "UI/TerrainEditToolbar.xml"))
  27.     self.filterui=ui:LoadLayout(cache:GetResource("XMLFile", "UI/TerrainEditFilters.xml"))
  28.     self.filterlist=self.filterui:GetChild("FilterList", true)
  29.     self.filteroptions=self.filterui:GetChild("FilterOptions", true)
  30.    
  31.     local content=Window:new(context)
  32.     content.style=uiStyle
  33.     self.filteroptions.contentElement=content
  34.    
  35.    
  36.    
  37.    
  38.     self.heightbrush.style=uiStyle
  39.     self.blendbrush.style=uiStyle
  40.     self.maskbrush.style=uiStyle
  41.     self.newterrain.style=uiStyle
  42.     self.toolbar.style=uiStyle
  43.     self.smoothbrush.style=uiStyle
  44.     self.filterui.style=uiStyle
  45.    
  46.     self.brushpreview=Image(context)
  47.     self.brushpreview:SetSize(64,64,3)
  48.     self.brushtex=Texture2D:new(context)
  49.     self.brushtex:SetSize(0,0,0,TEXTURE_DYNAMIC)
  50.    
  51.     self.heightbrush:GetChild("BrushPreview",true).texture=self.brushtex
  52.     self.blendbrush:GetChild("BrushPreview",true).texture=self.brushtex
  53.     self.maskbrush:GetChild("BrushPreview",true).texture=self.brushtex
  54.     self.smoothbrush:GetChild("BrushPreview",true).texture=self.brushtex
  55.    
  56.     ui.root:AddChild(self.heightbrush)
  57.     ui.root:AddChild(self.blendbrush)
  58.     ui.root:AddChild(self.maskbrush)
  59.     ui.root:AddChild(self.smoothbrush)
  60.     ui.root:AddChild(self.newterrain)
  61.     ui.root:AddChild(self.toolbar)
  62.     ui.root:AddChild(self.filterui)
  63.    
  64.    
  65.     self.mode=0
  66.     self.blendbrush.visible=false
  67.     self.maskbrush.visible=false
  68.     self.newterrain.visible=false
  69.     self.smoothbrush.visible=false
  70.     self.filterui.visible=false
  71.     self.toolbar.visible=true
  72.    
  73.    
  74.     self:SubscribeToEvent("Pressed", "TerrainEditUI:HandleButtonPress")
  75.     self:SubscribeToEvent("SliderChanged", "TerrainEditUI:HandleSliderChanged")
  76.     self:SubscribeToEvent("Toggled", "TerrainEditUI:HandleToggled")
  77.     self:SubscribeToEvent("ItemSelected", "TerrainEditUI:HandleItemSelected")
  78.    
  79.     self.brushcursornode=scene_:CreateChild()
  80.     self.brushcursor=self.brushcursornode:CreateComponent("CustomGeometry")
  81.     self.brushcursor:SetNumGeometries(1)
  82.     self.brushmat=cache:GetResource("Material", "Materials/TerrainBrush.xml")
  83.     self.brushmat:SetTexture(0, self.brushtex)
  84.     self.brushcursor:SetMaterial(self.brushmat)
  85.    
  86.     self.waypointpreview=scene_:CreateComponent("CustomGeometry")
  87.     self.waypointpreview:SetNumGeometries(1)
  88.     self.waypointpreviewmaterial=cache:GetResource("Material", "Materials/WaypointPreview.xml")
  89.     self.waypointpreview:SetMaterial(self.waypointpreviewmaterial)
  90.    
  91.     self.mode=0
  92.     self:ActivateHeightBrush()
  93.    
  94.     self:PopulateFilterList()
  95.    
  96.     self.counter=0
  97.     -- Waypoints
  98.     waypoints={}
  99. end
  100.  
  101. function TerrainEditUI:UpdateWaypointVis()
  102.     --print("1")
  103.     self.waypointpreview:Clear()
  104.     self.waypointpreview.occludee=false
  105.     self.waypointpreview:SetNumGeometries(1)
  106.     local c
  107.     local spacing=terrain:GetSpacing()
  108.     local plist=RasterVertexList()
  109.     for _,c in ipairs(waypoints) do
  110.         local pos=c.position
  111.         local norm=WorldToNormalized(hmap,terrain,pos)
  112.         local hx=math.floor(norm.x*hmap:GetWidth())
  113.         local hy=math.floor(norm.y*hmap:GetHeight())
  114.         local ht=GetHeightValue(hmap,hx,(hmap:GetHeight()-1)-hy)
  115.         plist:push_back(RasterVertex(hx,hy,ht))
  116.     end
  117.    
  118.     if plist:size()<4 then return end
  119.     --print("Num waypoints: "..plist:size())
  120.     local curve=RasterVertexList()
  121.     TessellateLineList(plist, curve, 10)
  122.     --print("Num curve points: "..curve:size())
  123.     local quad=RasterVertexList()
  124.     BuildQuadStrip(curve, quad, 8)
  125.     --print("Num quad points: "..quad:size())
  126.    
  127.     self.waypointpreview:BeginGeometry(0,TRIANGLE_LIST)
  128.     self.waypointpreview:SetDynamic(true)
  129.    
  130.     function buildVertex(rv)
  131.         local nx=rv.x_/hmap:GetWidth()
  132.         local ny=rv.y_/hmap:GetHeight()
  133.         local v=NormalizedToWorld(hmap,terrain,Vector2(nx,ny))
  134.         v.y=(rv.val_*255)*spacing.y
  135.         return v
  136.     end
  137.    
  138.     for c=0,quad:size()-4,2 do
  139.         self.waypointpreview:DefineVertex(buildVertex(quad:at(c)))
  140.         self.waypointpreview:DefineVertex(buildVertex(quad:at(c+1)))
  141.         self.waypointpreview:DefineVertex(buildVertex(quad:at(c+2)))
  142.        
  143.         self.waypointpreview:DefineVertex(buildVertex(quad:at(c+1)))
  144.         self.waypointpreview:DefineVertex(buildVertex(quad:at(c+2)))
  145.         self.waypointpreview:DefineVertex(buildVertex(quad:at(c+3)))
  146.         --print("hi")
  147.        
  148.     end
  149.    
  150.     self.waypointpreview:Commit()
  151.     self.waypointpreview:SetMaterial(self.waypointpreviewmaterial)
  152.     local bbox=self.waypointpreview.worldBoundingBox
  153.     bbox:Define(Vector3(-1000,-1000,-1000), Vector3(1000,1000,1000))
  154. end
  155.  
  156. function TerrainEditUI:AddWaypoint(groundx, groundz)
  157.     local waynode=scene_:CreateChild()
  158.     local model=waynode:CreateComponent("StaticModel")
  159.     model.material=cache:GetResource("Material", "Materials/Flag.xml")
  160.     model.model=cache:GetResource("Model", "Models/Flag.mdl")
  161.     model.castShadows=false
  162.     local ht=terrain:GetHeight(Vector3(groundx,0,groundz))
  163.     waynode.position=Vector3(groundx, ht, groundz)
  164.     waynode.scale=Vector3(0.25,0.25,0.25)
  165.     table.insert(waypoints, waynode)
  166.     self:UpdateWaypointVis()
  167. end
  168.  
  169. function TerrainEditUI:BuildFilterOptions(filter)
  170.     if filter==nil then return end
  171.     local options=self.filteroptions:GetChild("OptionsWindow", true)
  172.     local name=self.filteroptions:GetChild("FilterName", true)
  173.     local desc=self.filteroptions:GetChild("FilterDescription", true)
  174.     name.text=filter.name
  175.     desc.text=filter.description
  176.    
  177.     options:RemoveAllChildren()
  178.    
  179.     if filter.options==nil then print("No options") return end
  180.     local c
  181.     local maxx,maxy=0,0
  182.     for _,c in ipairs(filter.options) do
  183.         print("Option: "..c.name)
  184.         local window=Window:new(context)
  185.         window.defaultStyle=uiStyle
  186.         window.style=uiStyle
  187.         window.layoutMode=LM_HORIZONTAL
  188.         window.layoutBorder=IntRect(5,5,5,5)
  189.         local title=Text:new(context)
  190.         title.text=c.name
  191.         title.defaultStyle=uiStyle
  192.         title.style=uiStyle
  193.         --title.maxSize=IntVector2(64,0)
  194.         window:AddChild(title)
  195.        
  196.         if c.type=="flag" then
  197.             local check=CheckBox:new(context)
  198.             check.name=c.name
  199.             check.defaultStyle=uiStyle
  200.             check.style=uiStyle
  201.             if c.value==true then check.checked=true
  202.             else check.checked=false
  203.             end
  204.             window:AddChild(check)
  205.             window.size=IntVector2(title.size.x+check.size.x, 15)
  206.         elseif c.type=="value" then
  207.             local edit=LineEdit:new(context)
  208.             edit.name=c.name
  209.             edit.defaultStyle=uiStyle
  210.             edit.style=uiStyle
  211.             edit.textElement.text=tostring(c.value)
  212.             window:AddChild(edit)
  213.             window.size=IntVector2(title.size.x+edit.size.x, 15)
  214.         end
  215.         --if window.size.x > maxx then maxx=window.size.x end
  216.         window.maxSize=IntVector2(10000,25)
  217.         options:AddChild(window)
  218.     end
  219.    
  220.     --options.size.x=maxx
  221.     self.filteroptions.visible=true
  222. end
  223.  
  224. function TerrainEditUI:PopulateFilterList()
  225.     self.filters={}
  226.     self.selectedfilter=nil
  227.     local options=self.filteroptions:GetChild("OptionsWindow", true)
  228.     options:RemoveAllChildren()
  229.    
  230.     local list=self.filterlist:GetChild("List", true)
  231.     if list==nil then return end
  232.     list:RemoveAllItems()
  233.    
  234.     local filters=fileSystem:ScanDir(fileSystem:GetProgramDir().."/TerrainEditFilters", "*.lua", SCAN_FILES, false)
  235.     if filters==nil then print("Uh oh")
  236.     else
  237.         local c
  238.         for _,c in ipairs(filters) do
  239.             local filter=dofile("TerrainEditFilters/"..c)
  240.             print(c)
  241.             self.filters[filter.name]=filter
  242.             local uielement=Text:new(context)
  243.             uielement.style="EditorEnumAttributeText"
  244.             uielement.text=filter.name
  245.             uielement.name=filter.name
  246.             list:AddItem(uielement)
  247.         end
  248.     end
  249. end
  250.  
  251. function TerrainEditUI:HandleItemSelected(eventType, eventData)
  252.     local which=eventData["ListView"]
  253.     local selected=eventData:GetInt("Selection")
  254.     --local which=eventData["UIElement"]:GetPtr("Element")
  255.     --local entry=eventData["Selection"]:GetPtr("Element")
  256.     local entry=which:GetItem(selected)
  257.     --local entry = eventData["ListView"]:GetPtr("UIElement")
  258.     --local entry=which:GetItem(clicke)
  259.     if entry==nil then return end
  260.     local name=entry:GetName()
  261.    
  262.     if self.filters[name]==nil then return end
  263.    
  264.     self:BuildFilterOptions(self.filters[name])
  265.     self.selectedfilter=self.filters[name]
  266.    
  267.     --if self.filters[name] then
  268.         --self.filters[name]:execute()
  269.     --end
  270. end
  271.  
  272. function TerrainEditUI:GetBrushSettings(brush)
  273.     local power,max,radius,hardness=0,0,5,0.9
  274.     local usemask=false
  275.    
  276.     local slider
  277.     slider=brush:GetChild("PowerSlider", true)
  278.     if slider then power=(slider.value/slider.range)*4 end
  279.    
  280.     slider=brush:GetChild("MaxSlider", true)
  281.     if slider then max=(slider.value/slider.range) end
  282.    
  283.     slider=brush:GetChild("RadiusSlider", true)
  284.     if slider then radius=math.floor((slider.value/slider.range)*30) end
  285.    
  286.     slider=brush:GetChild("HardnessSlider", true)
  287.     if slider then hardness=(slider.value/slider.range) end
  288.    
  289.     local button=brush:GetChild("MaskCheck", true)
  290.     if button then usemask=button.checked end
  291.    
  292.     return power,max,radius,math.min(1,hardness),usemask
  293. end
  294.  
  295. function TerrainEditUI:BuildCursorMesh(radius)
  296.     self.brushcursor:BeginGeometry(0,TRIANGLE_LIST)
  297.     self.brushcursor:SetDynamic(true)
  298.     --self.brushcursor:SetMaterial(0, self.brushmat)
  299.    
  300.     local spacing=terrain:GetSpacing()
  301.     local spacingx=spacing.x
  302.     local spacingz=spacing.z
  303.     local meshsize=math.floor(radius)*2+2
  304.     local originx=(-meshsize/2)*spacingx
  305.     local originz=(-meshsize/2)*spacingx
  306.    
  307.     local uvspacing=1/(meshsize-1)
  308.    
  309.     local x,z
  310.     for x=0,meshsize-2,1 do
  311.         for z=0,meshsize-2,1 do
  312.             self.brushcursor:DefineVertex(Vector3(originx+x*spacingx, 0, originz+z*spacingz))
  313.             self.brushcursor:DefineTexCoord(Vector2(x*uvspacing, z*uvspacing))
  314.            
  315.             self.brushcursor:DefineVertex(Vector3(originx+(x+1)*spacingx, 0, originz+(z+1)*spacingz))
  316.             self.brushcursor:DefineTexCoord(Vector2((x+1)*uvspacing, (z+1)*uvspacing))
  317.            
  318.             self.brushcursor:DefineVertex(Vector3(originx+x*spacingx, 0, originz+(z+1)*spacingz))
  319.             self.brushcursor:DefineTexCoord(Vector2(x*uvspacing, (z+1)*uvspacing))
  320.            
  321.             self.brushcursor:DefineVertex(Vector3(originx+x*spacingx, 0, originz+z*spacingz))
  322.             self.brushcursor:DefineTexCoord(Vector2(x*uvspacing, z*uvspacing))
  323.            
  324.             self.brushcursor:DefineVertex(Vector3(originx+(x+1)*spacingx, 0, originz+z*spacingz))
  325.             self.brushcursor:DefineTexCoord(Vector2((x+1)*uvspacing, z*uvspacing))
  326.            
  327.             self.brushcursor:DefineVertex(Vector3(originx+(x+1)*spacingx, 0, originz+(z+1)*spacingz))
  328.             self.brushcursor:DefineTexCoord(Vector2((x+1)*uvspacing, (z+1)*uvspacing))
  329.         end
  330.     end
  331.    
  332.     self.brushcursor:Commit()
  333.    
  334.     self.brushcursor:SetMaterial(0, self.brushmat)
  335. end
  336.  
  337. function TerrainEditUI:SetBrushCursorHeight()
  338.     local mousepos
  339.     if input.mouseVisible then
  340.         mousepos=input:GetMousePosition()
  341.     else
  342.         mousepos=ui:GetCursorPosition()
  343.     end
  344.    
  345.     local ground=cam:GetScreenGround(mousepos.x, mousepos.y)
  346.    
  347.     --[[local numverts=self.brushcursor:GetNumVertices(0)
  348.    
  349.     local v
  350.     for v=0,numverts-1,1 do
  351.         local vert=self.brushcursor:GetVertex(0,v).position
  352.         local ht=terrain:GetHeight(Vector3(vert.x+ground.x,0,vert.z+ground.z))
  353.         vert.y=ht
  354.     end
  355.    
  356.     self.brushcursor:Commit()]]
  357.     SetBrushCursorHeight(terrain, self.brushcursor, ground.x, ground.z)
  358. end
  359.  
  360. function TerrainEditUI:ActivateHeightBrush()
  361.     self.power, self.max, self.radius, self.hardness, self.usemask=self:GetBrushSettings(self.heightbrush)
  362.    
  363.     self:BuildCursorMesh(self.radius)
  364.     self:GenerateBrushPreview(self.hardness)
  365.     self.heightbrush:GetChild("BrushPreview",true):SetTexture(self.brushtex)
  366.     self.heightbrush:GetChild("BrushPreview",true):SetImageRect(IntRect(0,0,63,63))
  367.     self.heightbrush.visible=true
  368.     self.blendbrush.visible=false
  369.     self.maskbrush.visible=false
  370.     self.smoothbrush.visible=false
  371.    
  372.     self.activebrush=self.heightbrush
  373.    
  374.     local text=self.activebrush:GetChild("PowerText", true)
  375.     if text then text.text=string.format("%.1f", self.power) end
  376.     text=self.activebrush:GetChild("RadiusText", true)
  377.     if text then text.text=tostring(math.floor(self.radius)) end
  378.     text=self.activebrush:GetChild("MaxText", true)
  379.     if text then text.text=string.format("%.1f", self.max) end
  380.     text=self.activebrush:GetChild("HardnessText", true)
  381.     if text then text.text=string.format("%.2f", self.hardness) end
  382.    
  383. end
  384.  
  385. function TerrainEditUI:ActivateBlendBrush()
  386.     self.power, self.max, self.radius, self.hardness, self.usemask=self:GetBrushSettings(self.blendbrush)
  387.    
  388.     self:BuildCursorMesh(self.radius)
  389.     self:GenerateBrushPreview(self.hardness)
  390.     self.blendbrush:GetChild("BrushPreview",true):SetTexture(self.brushtex)
  391.     self.blendbrush:GetChild("BrushPreview",true):SetImageRect(IntRect(0,0,63,63))
  392.     self.heightbrush.visible=false
  393.     self.blendbrush.visible=true
  394.     self.maskbrush.visible=false
  395.     self.smoothbrush.visible=false
  396.    
  397.     self.activebrush=self.blendbrush
  398.     local text=self.activebrush:GetChild("PowerText", true)
  399.     if text then text.text=string.format("%.1f", self.power) end
  400.     text=self.activebrush:GetChild("RadiusText", true)
  401.     if text then text.text=tostring(math.floor(self.radius)) end
  402.     text=self.activebrush:GetChild("MaxText", true)
  403.     if text then text.text=string.format("%.1f", self.max) end
  404.     text=self.activebrush:GetChild("HardnessText", true)
  405.     if text then text.text=string.format("%.2f", self.hardness) end
  406. end
  407.  
  408. function TerrainEditUI:ActivateSmoothBrush()
  409.     self.power, self.max, self.radius, self.hardness, self.usemask=self:GetBrushSettings(self.smoothbrush)
  410.    
  411.     self:BuildCursorMesh(self.radius)
  412.     self:GenerateBrushPreview(self.hardness)
  413.     self.blendbrush:GetChild("BrushPreview",true):SetTexture(self.brushtex)
  414.     self.blendbrush:GetChild("BrushPreview",true):SetImageRect(IntRect(0,0,63,63))
  415.     self.heightbrush.visible=false
  416.     self.blendbrush.visible=false
  417.     self.maskbrush.visible=false
  418.     self.smoothbrush.visible=true
  419.    
  420.     self.activebrush=self.smoothbrush
  421.     local text=self.activebrush:GetChild("PowerText", true)
  422.     if text then text.text=string.format("%.1f", self.power) end
  423.     text=self.activebrush:GetChild("RadiusText", true)
  424.     if text then text.text=tostring(math.floor(self.radius)) end
  425.     text=self.activebrush:GetChild("MaxText", true)
  426.     if text then text.text=string.format("%.1f", self.max) end
  427.     text=self.activebrush:GetChild("HardnessText", true)
  428.     if text then text.text=string.format("%.2f", self.hardness) end
  429. end
  430.  
  431. function TerrainEditUI:ActivateMaskBrush()
  432.     self.power, self.max, self.radius, self.hardness, self.usemask=self:GetBrushSettings(self.maskbrush)
  433.    
  434.     self:BuildCursorMesh(self.radius)
  435.     self:GenerateBrushPreview(self.hardness)
  436.     self.blendbrush:GetChild("BrushPreview",true):SetTexture(self.brushtex)
  437.     self.blendbrush:GetChild("BrushPreview",true):SetImageRect(IntRect(0,0,63,63))
  438.     self.heightbrush.visible=false
  439.     self.blendbrush.visible=false
  440.     self.maskbrush.visible=true
  441.     self.newterrain.visible=false
  442.     self.smoothbrush.visible=false
  443.    
  444.     self.activebrush=self.maskbrush
  445.     local text=self.activebrush:GetChild("PowerText", true)
  446.     if text then text.text=string.format("%.1f", self.power) end
  447.     text=self.activebrush:GetChild("RadiusText", true)
  448.     if text then text.text=tostring(math.floor(self.radius)) end
  449.     text=self.activebrush:GetChild("MaxText", true)
  450.     if text then text.text=string.format("%.1f", self.max) end
  451.     text=self.activebrush:GetChild("HardnessText", true)
  452.     if text then text.text=string.format("%.2f", self.hardness) end
  453. end
  454.  
  455.  
  456.  
  457. function TerrainEditUI:Update(dt)
  458.     self.counter=self.counter+dt
  459.     if self.counter>4 then
  460.         self.counter=self.counter-4
  461.         print("Used mem: "..collectgarbage("count"))
  462.         collectgarbage()
  463.     end
  464.     local mousepos
  465.     if input.mouseVisible then
  466.         mousepos=input:GetMousePosition()
  467.     else
  468.         mousepos=ui:GetCursorPosition()
  469.     end
  470.    
  471.     local ground=cam:GetScreenGround(mousepos.x, mousepos.y)
  472.    
  473.     if ground then
  474.         local world=Vector3(ground.x,0,ground.z)
  475.         self.brushcursornode:SetPosition(world)
  476.    
  477.         self.power, self.max, self.radius, self.hardness, self.usemask=self:GetBrushSettings(self.activebrush)
  478.    
  479.         self:SetBrushCursorHeight()
  480.     end
  481.  
  482.    
  483.     if input:GetMouseButtonDown(MOUSEB_LEFT) and ui:GetElementAt(mousepos.x, mousepos.y)==nil then
  484.         -- If CTRL is down and we are in mode==0 then grab the terrain height at the cursor instead
  485.         if self.mode==0 and input:GetQualifierDown(QUAL_CTRL) then
  486.             local ground=cam:PickGround(mousepos.x, mousepos.y)
  487.             if ground~=nil then
  488.                 local norm=WorldToNormalized(hmap,terrain,ground)
  489.                 --local tx=math.floor(norm.x*hmap:GetWidth()-1)
  490.                 --local ty=math.floor(norm.y*hmap:GetHeight()-1)
  491.                 --local col=hmap:GetPixel(tx,hmap:GetHeight()-ty)
  492.                 local col=hmap:GetPixelBilinear(norm.x,1-norm.y)
  493.                 local ht=0
  494.                 if hmap.components==1 then ht=col.r
  495.                 else ht=col.r+col.g/256.0
  496.                 end
  497.                 print(ht)
  498.                
  499.                 local slider=self.activebrush:GetChild("MaxSlider", true)
  500.                 if slider then slider.value=ht*slider.range end
  501.                 self.power, self.max, self.radius, self.hardness, self.usemask=self:GetBrushSettings(self.activebrush)
  502.                 --self:BuildCursorMesh(self.radius)
  503.                 self:GenerateBrushPreview(self.hardness)
  504.             end
  505.         else
  506.             local ground=cam:GetScreenGround(mousepos.x, mousepos.y)
  507.             if ground~=nil then
  508.                 local gx,gz=ground.x,ground.z
  509.            
  510.                 --self.edit:ApplyBrush(gx,gz, self.radius, self.max, self.power, self.hardness, self.mode, self.usemask, dt)
  511.                 if self.mode==0 then ApplyHeightBrush(terrain,hmap,mask,gx,gz,self.radius, self.max, self.power, self.hardness, self.usemask, dt) terrain:ApplyHeightMap()
  512.                 elseif self.mode>=1 and self.mode<=8 then ApplyBlendBrush8(terrain,hmap,blend1,blend2,mask,gx,gz,self.radius,self.max,self.power,self.hardness,self.mode-1,self.usemask,dt) blendtex1:SetData(blend1) blendtex2:SetData(blend2)
  513.                 elseif self.mode==9 then ApplySmoothBrush(terrain,hmap,mask,gx,gz,self.radius, self.max, self.power, self.hardness, self.usemask, dt) terrain:ApplyHeightMap()
  514.                 else ApplyMaskBrush(terrain,hmap,mask,gx,gz,self.radius,self.max,self.power,self.hardness,dt) masktex:SetData(mask)
  515.                 end
  516.             end
  517.            
  518.         end
  519.     elseif input:GetKeyPress(KEY_W) then
  520.         local mouseground=cam:PickGround(mousepos.x, mousepos.y)
  521.         self:AddWaypoint(mouseground.x, mouseground.z)
  522.        
  523.     elseif input:GetKeyPress(KEY_Q) then
  524.         if(#waypoints>0) then
  525.             waypoints[#waypoints]:Remove()
  526.             table.remove(waypoints)
  527.         end
  528.         self:UpdateWaypointVis()
  529.     end
  530.    
  531.     local c
  532.     for _,c in ipairs(waypoints) do
  533.         local ht=terrain:GetHeight(Vector3(c.position.x,0,c.position.z))
  534.         c.position=Vector3(c.position.x,ht,c.position.z)
  535.     end
  536.     self:UpdateWaypointVis()
  537. end
  538.  
  539. function TerrainEditUI:GenerateBrushPreview(sharpness)
  540.     local w,h=self.brushpreview:GetWidth(), self.brushpreview:GetHeight()
  541.     local rad=w/2
  542.     local x,y
  543.     for x=0,w-1,1 do
  544.         for y=0,h-1,1 do
  545.             local dx=x-w/2
  546.             local dy=y-h/2
  547.             local d=math.sqrt(dx*dx+dy*dy)
  548.             --local i=(rad-d)/rad
  549.             local i=(d-rad)/(sharpness*rad-rad)
  550.             i=math.max(0, math.min(1,i))
  551.                
  552.             self.brushpreview:SetPixel(x,y,Color(i*0.5,i*0.5,i*0.6))
  553.         end
  554.     end
  555.    
  556.     self.brushtex:SetData(self.brushpreview, false)
  557. end
  558.  
  559. function TerrainEditUI:HandleButtonPress(eventType, eventData)
  560.     local which=eventData["Element"]:GetPtr("UIElement")
  561.     local name=which:GetName()
  562.     if name=="HeightButton" then
  563.         self.mode=0    
  564.         self:ActivateHeightBrush()
  565.        
  566.     elseif name=="SmoothButton" then
  567.         self.mode=9
  568.         self:ActivateSmoothBrush()
  569.    
  570.     elseif name=="Terrain1Button" then
  571.         self.mode=1
  572.         self:ActivateBlendBrush()
  573.    
  574.     elseif name=="Terrain2Button" then
  575.         self.mode=2
  576.         self:ActivateBlendBrush()
  577.    
  578.     elseif name=="Terrain3Button" then
  579.         self.mode=3
  580.         self:ActivateBlendBrush()
  581.    
  582.     elseif name=="Terrain4Button" then
  583.         self.mode=4
  584.         self:ActivateBlendBrush()
  585.     elseif name=="Terrain5Button" then
  586.         self.mode=5
  587.         self:ActivateBlendBrush()
  588.     elseif name=="Terrain6Button" then
  589.         self.mode=6
  590.         self:ActivateBlendBrush()
  591.     elseif name=="Terrain7Button" then
  592.         self.mode=7
  593.         self:ActivateBlendBrush()
  594.     elseif name=="Terrain8Button" then
  595.         self.mode=8
  596.         self:ActivateBlendBrush()
  597.    
  598.    
  599.     elseif name=="MaskButton" then
  600.         self.mode=10
  601.         self:ActivateMaskBrush()
  602.    
  603.     elseif name=="FilterButton" then
  604.         if self.filterui.visible==true then self.filterui.visible=false
  605.         else
  606.             print("Showing filters")
  607.             self:PopulateFilterList()
  608.             self.filterui.visible=true
  609.         end
  610.     elseif name=="ExecuteButton" then
  611.         if self.selectedfilter then
  612.             -- Grab options
  613.             if self.selectedfilter.options ~= nil then
  614.                 local c
  615.                 for _,c in ipairs(self.selectedfilter.options) do
  616.                     local element=self.filteroptions:GetChild(c.name, true)
  617.                     if element then
  618.                         if c.type=="value" then c.value=tonumber(element.textElement.text)
  619.                         elseif c.type=="flag" then c.value=element.checked
  620.                         end
  621.                     end
  622.                 end
  623.             end
  624.             self.selectedfilter:execute()
  625.             collectgarbage()
  626.             print("Usedmem: "..collectgarbage("count"))
  627.         end
  628.     elseif name=="RescanFilters" then
  629.         self:PopulateFilterList()
  630.     elseif name=="ClearMask" then
  631.         mask:Clear(Color(1,1,1))
  632.         masktex:SetData(mask)
  633.     end
  634.    
  635. end
  636.  
  637. function TerrainEditUI:HandleSliderChanged(eventType, eventData)
  638.     local which=eventData["UIElement"]
  639.    
  640.     if which==nil then return end
  641.    
  642.     self.power, self.max, self.radius, self.hardness, self.usemask=self:GetBrushSettings(self.activebrush)
  643.     self:BuildCursorMesh(self.radius)
  644.     self:GenerateBrushPreview(self.hardness)
  645.    
  646.     if which==self.activebrush:GetChild("PowerSlider", true) then
  647.         local text=self.activebrush:GetChild("PowerText", true)
  648.         if text then text.text=string.format("%.2f", self.power) end
  649.     elseif which==self.activebrush:GetChild("RadiusSlider", true) then
  650.         local text=self.activebrush:GetChild("RadiusText", true)
  651.         if text then text.text=tostring(math.floor(self.radius)) end
  652.     elseif which==self.activebrush:GetChild("MaxSlider", true) then
  653.         local text=self.activebrush:GetChild("MaxText", true)
  654.         if text then text.text=string.format("%.2f", self.max) end
  655.     elseif which==self.activebrush:GetChild("HardnessSlider", true) then
  656.         local text=self.activebrush:GetChild("HardnessText", true)
  657.         if text then text.text=string.format("%.3f", self.hardness) end
  658.     end
  659. end
  660.  
  661. function TerrainEditUI:HandleToggled(eventType, eventData)
  662.     local which=eventData["UIElement"]:GetPtr("Element")
  663.     if which==nil then return end
  664.        
  665. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement