% Create a randomized landscape
% Calls: Build, BuildCustomized, ChangeOptions
clear;
close;

% win1H contains, in order: main window, size1, size2, size3, iter1, iter2, iter3,
% replot, close, rand1, rand2, rand3, height1, height2, height3,
% decay1, decay2, decay3
% win2H contains, in order: custom window, smooth1, smooth2, jag1, jag2, jag3, trees1,
% trees2, detail1, detail2, detail3, water1, water2, water3
% mi contains the nine menu items

% Default variable values
method = 'simple';
sz = 15;
iters = 15;
randm = 0;
peak = 0;
decay = 0;
dim(1) = 0;

% Default variable values for custom window
jag = 0;
detail = 10;
water = 30;

%== The customizing window ==========================
win2H(1) = figure(2);
clf;

%== Window setup ===========================
set(win2H(1),'Position',[568,294,300,256],...
'Name','Lanscape Customizer',...
'Color','black',...
'NumberTitle','off',...
'Visible','off');

%== Smooth checkbox ===========================
win2H(2) = uicontrol(gcf,'Style','text',...
'String','Smooth landscape',...
'Position',[15,210,112,16],...
'BackgroundColor','black',...
'ForegroundColor','yellow',...
'FontSize',12);
win2H(3) = uicontrol(gcf,'Style','Checkbox', ...
'Position',[132,210,16,16],...
'BackgroundColor','black',...
'ForegroundColor','yellow',...
'Value',1,...
'Callback',...
['if get(win2H(3),''Value'')',...
'set(win2H(6),''Enable'',''off'');',...
'jag = 0;',...
'set(win2H(5),''String'',num2str(jag));',...
'set(win2H(6),''Value'',jag);',...
'else ',...
'set(win2H(6),''Enable'',''on'');',...
'end',...
] );

%== Jaggedness slider ===========================
win2H(4) = uicontrol(gcf,'Style','text', ...
'String','Jaggedness:',...
'Position',[15,140,74,20],...
'BackgroundColor','black',...
'ForegroundColor','yellow',...
'FontSize',12);
win2H(5) = uicontrol(gcf,'Style','text',...
'String',num2str(jag,'%1d'),...
'BackgroundColor','black',...
'ForegroundColor','yellow',...
'HorizontalAlignment','left',...
'FontSize',12,...
'Position',[94,140,56,20]);
win2H(6) = uicontrol(gcf,...
'Style','slider',...
'SliderStep',[.1 .5],...
'Min',0,'Max',10,...
'Position',[15,120,135,20],...
'Value',jag,...
'Enable','off',...
'CallBack',...
['jag = (get(win2H(6),''Value''));'...
'set(win2H(5),''String'',num2str(jag));'...
] );

%== Trees checkbox ===========================
win2H(7) = uicontrol(gcf,'Style','text',...
'String','Add trees',...
'Position',[180,210,61,16],...
'BackgroundColor','black',...
'ForegroundColor','yellow',...
'FontSize',12);
win2H(8) = uicontrol(gcf,'Style','Checkbox', ...
'Position',[246,210,16,16],...
'BackgroundColor','black',...
'ForegroundColor','yellow',...
'Value',0);

%== Detail slider ===========================
win2H(9) = uicontrol(gcf,'Style','text', ...
'String','Detail:',...
'Position',[15,50,42,20],...
'BackgroundColor','black',...
'ForegroundColor','yellow',...
'FontSize',12);
win2H(10) = uicontrol(gcf,'Style','text',...
'String',num2str(detail,'%1d'),...
'BackgroundColor','black',...
'ForegroundColor','yellow',...
'HorizontalAlignment','left',...
'FontSize',12, ...
'Position',[62,50,88,20]);
win2H(11) = uicontrol(gcf,...
'Style','slider',...
'SliderStep',[.1 .5],...
'Min',0,'Max',10, ...
'Position',[15,30,135,20],...
'Value',detail,...
'CallBack',...
['detail = (get(win2H(11),''Value''));'...
'set(win2H(10),''String'',num2str(detail));'...
] );

%== Water slider ===========================
win2H(12) = uicontrol(gcf,'Style','text', ...
'String','Percent water:',...
'Position',[165,140,93,20],...
'BackgroundColor','black',...
'ForegroundColor','yellow',...
'FontSize',12);
win2H(13) = uicontrol(gcf,'Style','text',...
'String',num2str(water,'%1d'),...
'BackgroundColor','black',...
'ForegroundColor','yellow',...
'HorizontalAlignment','left',...
'FontSize',12, ...
'Position',[263,140,37,20]);
win2H(14) = uicontrol(gcf,...
'Style','slider',...
'SliderStep',[.025 .25],...
'Min',30,'Max',70, ...
'Position',[165,120,135,20],...
'Value',water,...
'CallBack',...
['water = (get(win2H(14),''Value''));'...
'set(win2H(13),''String'',num2str(water));'...
] );

%== Done button ===========================
win1H(9) = uicontrol(gcf,'Style','Pushbutton', ...
'String','Done',...
'Position',[198,30,70,30],...
'BackgroundColor','black',...
'ForegroundColor','yellow',...
'CallBack',...
['set(win2H(1),''Visible'',''off'');',...
'figure(win1H(1));',...
'[method,sz,iters,randm,peak,decay,dim,jag,water] = BuildCustomized(win1H,win2H,dim);',...
] );

%== The main window ==========================
win1H(1) = figure(1);
clf;

%== Window setup ===========================
set(win1H(1),'Position',[50,50,500,500],...
'Name','Landscape Builder',...
'NumberTitle','off');

%== Drawing box ==============================
set(0,'DefaultAxesCreateFcn', ...
'[sz,iters,dim] = Build(method,sz,iters,randm,peak,decay,dim,jag,water,0);');
ah = axes('Position',[0.1 0.1, 0.8 0.75],...
'Box', 'on', ...
'Color','white',...
'XColor',[0,0,0],'YColor',[0,0,0]);
axis square;

%== Grid size slider ===========================
win1H(2) = uicontrol(gcf,'Style','text', ...
'String','Grid size',...
'Position',[15,470,90,20],...
'BackgroundColor','white', ...
'FontSize',12);
win1H(3) = uicontrol(gcf,'Style','text',...
'String',num2str(sz,'%1d'),...
'FontSize',12, ...
'BackgroundColor','white', ...
'Position',[15,450,90,20]);
win1H(4) = uicontrol(gcf,...
'Style','slider',...
'SliderStep',[.01 .1],...
'Min',5,'Max',55, ...
'Position',[15,430,90,20], ...
'Value',sz,...
'CallBack', ...
['sz = round(get(win1H(4),''Value''));' ...
'set(win1H(4),''Value'',sz);' ...
'set(win1H(3),''String'',num2str(sz));' ...
'[sz,iters,dim] = Build(method,sz,iters,randm,peak,decay,dim,jag,water,method == ''custom'');' ...
] );

%== Iterations slider ===========================
win1H(5) = uicontrol(gcf,'Style','text', ...
'String','Iterations',...
'Position',[114,470,90,20],...
'BackgroundColor','white', ...
'FontSize',12);
win1H(6) = uicontrol(gcf,'Style','text',...
'String',num2str(iters,'%1d'),...
'FontSize',12, ...
'BackgroundColor','white', ...
'Position',[114,450,90,20]);
win1H(7) = uicontrol(gcf,...
'Style','slider',...
'SliderStep',[.04 .2],...
'Min',1,'Max',26, ...
'Position',[114,430,90,20], ...
'Value',iters,...
'CallBack', ...
['iters = round(get(win1H(7),''Value''));' ...
'set(win1H(7),''Value'',iters);' ...
'set(win1H(6),''String'',num2str(iters));' ...
'[sz,iters,dim] = Build(method,sz,iters,randm,peak,decay,dim,jag,water,method == ''custom'');' ...
] );

%== Randomness slider ===========================
win1H(10) = uicontrol(gcf,'Style','text', ...
'String','Randomness',...
'Position',[312,470,90,20],...
'BackgroundColor','white', ...
'FontSize',12);
win1H(11) = uicontrol(gcf,'Style','text',...
'String',num2str(randm,'%1d'),...
'FontSize',12, ...
'BackgroundColor','white', ...
'Position',[312,450,90,20]);
win1H(12) = uicontrol(gcf,...
'Style','slider',...
'SliderStep',[.01 .1],...
'Min',0,'Max',10, ...
'Position',[312,430,90,20], ...
'Value',randm,...
'CallBack', ...
['randm = (get(win1H(12),''Value''));' ...
'set(win1H(11),''String'',num2str(randm));' ...
'[sz,iters,dim] = Build(method,sz,iters,randm,peak,decay,dim,jag,water,method == ''custom'');' ...
] );

%== High peaks slider ===========================
win1H(13) = uicontrol(gcf,'Style','text', ...
'String','High Peaks',...
'Position',[411,470,90,20],...
'BackgroundColor','white', ...
'FontSize',12);
win1H(14) = uicontrol(gcf,'Style','text',...
'String',num2str(peak,'%1d'),...
'FontSize',12, ...
'BackgroundColor','white', ...
'Position',[411,450,90,20]);
win1H(15) = uicontrol(gcf,...
'Style','slider',...
'SliderStep',[.01 .1],...
'Min',0,'Max',10, ...
'Position',[411,430,90,20], ...
'Value',peak,...
'CallBack', ...
['peak = (get(win1H(15),''Value''));' ...
'set(win1H(14),''String'',num2str(peak));' ...
'[sz,iters,dim] = Build(method,sz,iters,randm,peak,decay,dim,jag,water,method == ''custom'');' ...
] );

%== Decay slider ===========================
win1H(16) = uicontrol(gcf,'Style','text', ...
'String','Decay',...
'Position',[213,470,90,20],...
'BackgroundColor','white', ...
'FontSize',12);
win1H(17) = uicontrol(gcf,'Style','text',...
'String',num2str(decay,'%1d'),...
'FontSize',12, ...
'BackgroundColor','white', ...
'Position',[213,450,90,20]);
win1H(18) = uicontrol(gcf,...
'Style','slider',...
'SliderStep',[.01 .1],...
'Min',0,'Max',10, ...
'Position',[213,430,90,20], ...
'Value',decay,...
'CallBack', ...
['decay = (get(win1H(18),''Value''));' ...
'set(win1H(17),''String'',num2str(decay));' ...
'[sz,iters,dim] = Build(method,sz,iters,randm,peak,decay,dim,jag,water,method == ''custom'');' ...
] );

%== Dimension label ===========================
d = uicontrol(gcf,'Style','frame',...
'Position',[171,10,172,18]);
dim(2) = uicontrol(gcf,'Style','text', ...
'String','Fractal dimension:',...
'Position',[172,11,120,16],...
'BackgroundColor','white', ...
'FontSize',12);
dim(3) = uicontrol(gcf,'Style','text',...
'String',num2str(dim(1),'%1d'),...
'FontSize',12, ...
'BackgroundColor','white', ...
'Position',[292,11,50,16]);

%== Replot button ===========================
win1H(8) = uicontrol(gcf,'Style','Pushbutton', ...
'String','Replot',...
'Position',[35,10,70,30],...
'BackgroundColor','white', ...
'CallBack', ...
'[sz,iters,dim] = Build(method,sz,iters,randm,peak,decay,dim,jag,water,method == ''custom'');' );

%== Close button ===========================
win1H(9) = uicontrol(gcf,'Style','Pushbutton', ...
'String','Close',...
'Position',[410,10,70,30],...
'BackgroundColor','white', ...
'CallBack', ...
['close;',...
'figure(win2H(1));',...
'close;',...
] );

%== Method Menu ===========================
m1 = uimenu (gcf,'Label','Method') ;
mi(1) = uimenu (m1, 'Label','Simple', ...
'Checked','on',...
'CallBack', 'method = ''simple''; [sz,iters,dim] = ChangeOptions(method,sz,iters,randm,peak,decay,dim,2,30,mi,win1H);');
mi(2) = uimenu (m1, 'Label','Fractal', ...
'CallBack','method = ''fractl''; [sz,iters,dim] = ChangeOptions(method,sz,iters,randm,peak,decay,dim,2,30,mi,win1H);');
mi(3) = uimenu (m1, 'Label','Subdivided', ...
'CallBack', 'method = ''subdiv''; [sz,iters,dim] = ChangeOptions(method,sz,iters,randm,peak,decay,dim,2,30,mi,win1H);');
mi(4) = uimenu (m1, 'Label','Spline', ...
'CallBack','method = ''spline''; [sz,iters,dim] = ChangeOptions(method,sz,iters,randm,peak,decay,dim,2,30,mi,win1H);');
mi(5) = uimenu (m1, 'Label','Spline with Superimposition', ...
'CallBack', 'method = ''spplus''; [sz,iters,dim] = ChangeOptions(method,sz,iters,randm,peak,decay,dim,2,30,mi,win1H);');
mi(6) = uimenu (m1, 'Label','Randomized', ...
'CallBack','method = ''random''; [sz,iters,dim] = ChangeOptions(method,sz,iters,randm,peak,decay,dim,2,30,mi,win1H);');
mi(7) = uimenu (m1, 'Label','Spline with Randomized Superimposition', ...
'CallBack', 'method = ''randsp''; [sz,iters,dim] = ChangeOptions(method,sz,iters,randm,peak,decay,dim,2,30,mi,win1H);');
mi(8) = uimenu (m1, 'Label','Randomized with Randomized Superimposition', ...
'CallBack','method = ''rndpls''; [sz,iters,dim] = ChangeOptions(method,sz,iters,randm,peak,decay,dim,2,30,mi,win1H);');
mi(9) = uimenu (m1, 'Label','CUSTOMIZED', ...
'CallBack','method = ''custom''; [sz,iters,dim] = ChangeOptions(method,sz,iters,randm,peak,decay,dim,jag,water,mi,win1H,win2H);');